简体   繁体   中英

Get currently running thread objects from ExecutorService

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM