Reputation: 20391
I did some reading of other post but didn't find an exact answer to what I'm looking for, so I hope someone can give some some clarification.
I have a program that will run for some time. I have some threads that run in the back ground that perform various tasks, to keep things simple let think of 3 threads. ThreadA
performs a task every 10 seconds, where ThreadB
does something every 30 seconds and ThreadC
does something every 5 mintues.
I don't use busy waiting, and put the threads to sleep for the designated times.
My question is regarding a clean shut down. I have a variable that each of the threads have read access too, so they can see when the user initiates the exit of the program. The next time the threads are active, they exit their loops and join and all is good. But you can see that ThreadC only wakes up every 5 minutes.
The question I have is can I signal the sleeping threads to wake up and exit before their sleep time is over? If this is not possible, do I need to rework the code to use wait()
and notify()
or is there a better way?
Upvotes: 8
Views: 28294
Reputation: 79
How will you make a difference between do an action or shutdown the thread? Set an action code into the thread and interrupt the thread is not atomic. Between this two action may raise an additional interrupt from a system for instance and this info will be lost, because the thread see only the action code after an interrupt, but don't care about how many interrupt happened.
So, I think the thread interrupt shall be used only for gracefully finish a thread.
Upvotes: 0
Reputation: 27190
All of the other answers are good answers to the question that you actually asked, but none of them gives the best answer to the question that you should have asked. The question is, what's the best way to schedule several periodic tasks that must recur with different periods? The answer is, use a java.util.concurrent.ScheduledExecutorService.
Like all ExeuctorServices, it provides methods, shutdown() and shutdownNow(), for when you want the tasks to stop.
Upvotes: 1
Reputation: 7576
You should look into ExecutorService and its shutdownNow() method (which generally interrupts all the active threads). You sense reinventing the scheduling wheel.
Upvotes: 3
Reputation: 26198
You can call the interrupt()
method on your thread and will make your Thread go to running state
from block state
, but it will throw an exception which is InterruptedException
which by then you can shutdown your thread in the catch
block
Also other solution is that you can use the Timer Task that will call the run method on a certain time you specified without putting your Thread to the block state
example:
public class TimerBomb {
Toolkit toolkit;
Timer timer;
int count = 0;
public TimerBomb(int seconds) {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.schedule(new RemindTask(), seconds * 1000, seconds*1000);
}
class RemindTask extends TimerTask {
public void run() {
//do stuff here
}
}
public static void main(String args[]) {
System.out.println("About to schedule task.");
Thread t = new Thread(new Runnable() {
@Override
public void run() {
new TimerBomb(5);
}
});
t.start();
System.out.println("Task scheduled.");
}
}
It will run every 5 second forever.
Upvotes: 5
Reputation: 10308
If the threads are sleeping with Thread.sleep(...)
, you can wake them up with Thread.interrupt()
. Make sure you're handling the InterruptedException
and testing Thread.currentThread().isInterrupted()
in your loops.
Upvotes: 7
Reputation: 26185
Thread.sleep
throws InterruptedException
if the thread is interrupted during the sleep. Catch that, and check your flag.
Upvotes: 12