I currently have a threadpool with 2 fixed threads and each thread creates 2 more threads that perform task. I have it set up to where I can pass commands to stop a thread if needed.
What I'm asking is if there is a way to select a specific fixed thread from the threadpool and shut it down.
I have everything set up to shutdown the thread just need a way to select one of the two threads and shut it down and have the other one continue running.
If there is a better way to do this I'm open to other options.
Thanks
What I'm asking is if there is a way to select a specific fixed thread from the threadpool and shut it down.
Not from the pool itself, no. Remember that you don't want to kill the thread in a thread-pool since there may be more tasks to execute.
If there is a better way to do this I'm open to other options.
I'd have a volatile boolean that is being checked in the task in question so you can cause it to quit.
private volatile boolean shutdownSpecificTask;
...
// then inside of your task you'd do something like
while (!shutdownSpecificTask) {
...
}
The only operations like this that you have at the thread-pool level is to interrupt all of the running threads with a shutdownNow() or a Future.cancel(true). Both of these interrupt the thread which sets the interrupt flag and cause methods that throw InterruptedException to do so.
Related
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.
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.
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.
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.
I have made a java program with GUI and have placed a "stop" button on it. When I start the program, the main thread starts 10 threads. Now I want that whenever a user click on "stop" button, all the threads should terminate first, then the main thread should terminate. How can I do that.
It depends on how you want to the 10 threads to "terminate", and how you are running the threads.
I recommend creating an ExecutorService, and writing your "threads" as Runnable implementations. These Runnable objects should respond to calls to interrupt() on their thread of execution by aborting their task, cleaning up, and exiting the run method as quickly as possible.
Submit these Runnable tasks to an ExecutorService then call awaitTermination, all in the main thread. When the user presses the "stop" button, call shutdown or shutdownNow as desired on the ExecutorService (from the event dispatch thread).
If you call shutdownNow on the executor service, it will notify the running tasks by interrupting their threads. If you call shutdown, it will allow the tasks to complete without interruption. Regardless, your main thread will block on awaitTermination until all of the tasks have completed (or the time limit runs out).
You can, of course, create and manage of all of the threads yourself, using join. The key is to make the threads interruptible if you want to be able to stop them prematurely.
Firstly, let me note that there is a tempting method on the Thread class called stop(). Do not use it, it is dangerous.
One way of doing this is to code your 10 threads to check the interrupted status of the thread.
e.g.
while (! Thread.interrupted())
{
// Do your thread's work
}
You can interrupt each worker thread by calling the interrupt() method on the Thread object, and then calling join() to wait for the thread to actually finish.
Give all Threads a shared instance of a Boolean and let them check periodically if it is true. If it's true the Thread should return from its run method. Set this Boolean to true if the user presses the stop button and then use Thread.join() to wait for all Threads.
Thread.stop() is deprecated, and it's adviced that you use a 'running' flag that is periodically checked by the Thread so it can terminate itself when you set the the flag to false. If the Thread has long wait phases, you can interrupt() it to wake it up from a wait() state.
-> Thread.stop() int he Java API doc
After terminating your Threads by setting the running condition to false, you can have your main Thread join() the other Threads sequentially to wait for their termination.
The suspend() and resume() methods on the Thread class are deprecated because they are inherently unsave. Look at this article for information on why they were deprecated and techniques to stop threads:
http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html
If your threads are waiting on an InputStream then just add some boolean flag to threads and close the stream. Threads will wakeup, check the flag and exit. Same if they are waiting on a Condition (Object.wait()), use notifyAll() and set the flag. If your threads are constantly looping (which is BAD), just set the flag :)