Jatin
Jatin

Reputation: 43

How to stop executor service threads even when their task is not completed?

I started some threads using Executor Service for getting some files from network.I want the Threads to stop execution after some time duration even if their run method is not completed. How to do that? Even executors' shutdown() and awaitTermination(...) methods did not work.

Upvotes: 3

Views: 1348

Answers (1)

Chris Cooper
Chris Cooper

Reputation: 5112

The simple answer is that you can't. Thread.stop() was deprecated a long (long long) time ago because it was unreliable and was prone to leaving dirty resources behind.

The only other thing you can do, is have your thread pause during its workload and check for exit conditions (like being interrupted, or exceeding some predefined time limit).

Upvotes: 6

Related Questions