Graceful stop of threads