1

I am using ExecutorService(Globally declared) to run multithread at the same time. ExecutorService will take one hour to complete the task. So I need to get the currently running thread details like active thread, queued thread and completed thread.

And when I kill the ExecutorService , the ExecutorService killed but the thread running in background. So before kill the ExecutorService , I need to get the thread object form ExecutorService and kill the thread manually using loop or iterator.

For Example:

ExecutorService has 5 threads.

Each thread has execute by invokeall(). Each thread will end after one hour.

But thing is I need to stop within 15 minutes by sending new request to server or call some method.

Here shutdown or shutdowNow stop the executor but not those 5 threads. That is the problem. So I need to kill those threads manually. How to kill the threads manually attached in executor service?

GopalKrish
  • 25
  • 1
  • 7
  • Normally your main thread will call `awaitTermination()` on the main thread which will effectively join it and prevent termination prior to the jobs completing. Do you have any code that you've tried so far? – Craig Otis Nov 28 '16 at 14:41
  • 1
    Possible duplicate of [How to wait for all threads to finish, using ExecutorService?](http://stackoverflow.com/questions/1250643/how-to-wait-for-all-threads-to-finish-using-executorservice) – Sikorski Nov 28 '16 at 14:42
  • Could you clarify : do you need to get the *threads* or the *tasks* themselves, and why ? As other comments show, there are way to know if an executor has finished its work, get the number of pending tasks, ... and if this is your goal, then threads are actually **not** the good unit of abstraction to work with at all. Plus you can not kill and executor service, you can only shut it down, which has a different meaning for what happens to its tasks, vs. its threads. There are also no way to kill threads in Java (only deprecated, unreliable ways) – GPI Nov 28 '16 at 15:24
  • See: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html#shutdownNow(). It indicates most implementations will attempt to kill running threads by calling Thread.interrupt(). The problem is likely that your threads are ignoring interrupt. Unless those threads make system calls that perform that check, you need to check for interrupt manually and respond accordingly. – Paul Jackson Nov 29 '16 at 06:38

1 Answers1

0

I found the solution, all threads are append in Callable, and invoke the Callable threads by using ExecutorService. ExecutorService only contains the value of active, queue and completed details. After execution the result will fetch the value using Future class.

Now, If I want to stop the execution before completion. I need to invoke callable object and convert into thread object and stop manually if thread not null. So Callable object also change as global variable. End execution remove those objects.

GopalKrish
  • 25
  • 1
  • 7