How to gracefully stop a Java ExecutorService’s troubled thread? - java

I am submitting callables to executorService (fixedThreadPool).
and storing the reference of the thread in callable object.
now the problem here is, in these threads, I am calling someone else's callback method(consider we have a jar with one interface and that interface's implementation is done by the targeted product), so basically I have no control on what other product is doing in that thread.
so sometimes what happens is due to some socket connection issues this thread gets stuck for an indefinite time and now, here is the issue I do not want to wait for that thread to end I want to kill that thread gracefully.
I tried with thread.interrupt() but it is only able to interrupt blocked, waiting and sleeping threads. The runnable or working thread can not be interrupted by this.
(Note: I am able to kill this thread forcefully using thread.stop() but as that is not recommended I do not want to do that, I am searching for a graceful solution)
now let us say it is ok that I am not able to kill this thread due to some limitations but the problem here is if I call future.cancel(true) on my future task I am not able to free this thread from my executor pool, basically I can not reuse that executor task anymore(here basically I have a static reference of executor service which has been reused in the loop), I have even tried using executorService.shutdown and shutdownNow nothing was able to free that thread from executor service.
so is there any way to kill this thread gracefully and free it from executor service.

Related

How do I interrupt a long running Java Thread in a ThreadPoolExecutor

Here is my situation:
I have a ThreadPoolExecutor on whose Threads I make a HTTP call to a server via SSL. Sometimes, I get a NullPointerException because the client cannot decrypt the server response and the thread just hangs there indefinitely causing a livelock in the pool. Does anybody know a good way to target a specific thread in the pool, check how long it's been running and kill/interrupt it if it's been running for longer than say 3 minutes?
Any suggestions welcome.
If you keep a reference to that thread, then you can call interrupt() method on it in order to interrupt it. for example:
Thread t = new Theard(r);
t.interrupt();
(r is runnable, a job)
With Spring aop you can count the time passed from the moment that start method was triggered.
Another solution is to use your own thread factory, in the factory make sure to interrupt each thread after 3 minutes.
Another approach is to use: ScheduledThreadPoolExecutor
set your task to run for a period of time, and let it end gracefully after one execution.

Java concurrency – Immediate thread pool shutdown

I am creating thread pools like this:
ExecutorService workers = Executors.newCachedThreadPool();
Invoking each pool tasks like this:
workers.invokeAll(tasks);
And after completion shutting those down like this:
workers.shutdown();
I have about 4 thread pools that do different procedures and those thread pools are being created from a servlet class.
What I want to do is shutdown all threads in those thread pools.
What is the cleanest way to achieve this?
Thanks
If all your worker tasks handle interrupts properly you could try to invoke:
workers.shutdownNow()
That call with typically send interrupts too all worker threads. However, proper interrupt handling is a bit implicit and the method documentation says that only a best effort attempt to stop the tasks is made. Hence, some JVM implementations might make a worse attempt than sending interrupts, why you might not want to trust this call.
You might want to look into other answers how to gracefully ensure proper shutdown of threads and implement such a solution for all your worker tasks, to guarantee proper shutdown. For example, in this answer, Jack explains the typical solution to have a volatile field that you can check in your workers. This field can be set from where you want to stop your tasks.

Trickling down an interruption (kill -15 or 9) signal (Ctrl + C) from ScheduledThreadPoolExecutor --> ExecutorService in java

I have been reading a lot on this but I am not sure what's the most elegant way of handling my usecase. I have an application that starts a background scheduled thread using ScheduledThreadPoolExecutor. This scheduled thread in-turn has an ExecutorService of pool size 20. Each new thread submitted to this pool will inturn again have an ExecutorService of pool size, lets say 50. The lowest level thread doesn't do much other than looping through some standard tasks, each task taking anywhere from a second to 10 seconds.
As this is a background agent application performing background tasks, We should be able to stop them cleanly any time we want. The problem is I am not sure how to trickle down the an interuption/shutdown signal 3 level down to the lowest thread so I can break out of the loop and shutdown all the threads neatly.
I was looking into Runtime.addShutdownHook() but I wasn't exactly sure how it will be useful in my usecase. I was also looking into checking for isInterrupted() at the lowest possible Thread level but than I wasn't sure if Ctrl + C or kill -9 / kill -15 command actually is transformed to an interrupted signal inside the application. And if so, how would it trickle down 3 levels of threads, or would I have to manually interrupt each thread inside the Runtime.addShutdownHook().
I am trying to find a solution that is most elegant and safe.
The interrupted flag has nothing to do with native OS-level signals sent to the process hosting the JVM. You can set the interrupted flag on any thread by calling thread.interrupt().
For your problem I would suggest accumulating all your ExecutorServices into a global collection so that you may call shutdownNow() on each upon termination. If you use a gentle-enough signal to terminate your process, the shutdown hooks should be executed and there you can try to shut down your executor services. Note, however, that each task you submit must be interruptible, which means that it must respoond to the setting of the interrupted flag by actually finishing its work. This will not happen implicitly.
I must add that I find your solution with numerous executor services quite odd. A single, properly configured thread pool should be all you need in addition to the scheduled executor.

thread blocking in fixed threadpool

I have two doubts:
Regarding fixed threadpool in Java. Assume I created a fixed threadpool with 5 threads and all threads are currently executing, and also assume there are 4 task waiting in the queue to finish the execution of these threads. If all currently executing tasks got blocked what will happen? Whether there is a way to take task from that queue and put the blocked task in queue?
How we will come to know whether a task is blocked or executing?
If all currently executing tasks got blocked what will happen? Whether there is a way to take task from that queue and put the blocked task in queue?
No, there's no facility for this. If a task begins executing and is blocked, then will block that thread until it completes normally, or is interrupted by a thread pool shutdown.
How we will come to know whether a task is blocked or executing?
If you need to know this, then you need to put some knowledge into the task code itself, which can then be queried by some other part of your application. Obviously, something else will need to keep hold of a reference to the task to allow this to work, before submitting it to the executor.

java - what happens to runnables that hang in threadpoolexecutor?

If a runnable hangs while running in a threadpoolexecutor, is there a way to find out that it has hung and kill the runnable? Will the getActiveCount method consider a runnable that's hanging as "actively executing"?
There is no safe way to kill a thread which is busy (other than running it in another process and killing it) You can detect if a thread is taking to long by waiting for the result with a timeout. You can also add a task to cancel the task after a timeout, however this will only interrupt a thread's task, not kill it.
You are better off determining why the task "hangs" and fixing the code so it doesn't.
When you start a task you store Thread.currentThread() is a share variable. You can then take a getStackTrace() periodically to determine what it is doing and log it.

Categories

Resources