Stopping a Timer Thread

     

As we have seen, timer tasks will run forever if you don't shut down the thread. So there are a few different ways we can stop a timer task that's running.

  1. Unplug the machine.

  2. Call System.exit(0) , which shuts down all of the threads in an app.

  3. Create the timer as a daemon thread. Doing so will automatically stop the thread once there are only daemon threads left executing, and not user threads. See the "Creating a TimerTask as a Daemon" section later.

  4. After all of the tasks have finished executing, remove all of the references to the Timer object. This is not a great way to do this if you need specific control over the cessation of this task, because when it is garbage collected, the thread terminates. But you aren't guaranteed of knowing when exactly that will be.

  5. Call the Timer object's cancel() method. That method terminates the timer and throws away any tasks that are currently scheduled. After you cancel a timer in this manner, no more tasks may ever be scheduled on it.

Now that we see the different ways to do it, let's look at an example that offers us the most control, which is a good thing: calling the timer.cancel() method.

StoppableTask.java

 

 package net.javagarage.tasks; import java.util.Timer; import java.util.TimerTask; /**  * <p>  * Shows that we can extend TimerTask and just  * override the run method there to do the work.  * The benefit of this over doing it the other  * way is that anonymous inner classes don't have  * access to the non-final variables of the enclosing  * class, so we can't call timer.cancel and such.  * Here we can call timer.cancel(), which is what  * we're trying to do.  *  * @author eben hewitt  */ public class StoppableTask { int delay = 0; //no delay int interval = 2000; //run every two seconds Timer timer; //constructor public StoppableTask() { //make thread a daemon thread timer = new Timer(); System.out.println("Starting task"); timer.schedule(new SuperTask(), delay, interval); } public static void main(String[] args) { new StoppableTask(); } // this inner class guy does whatever the //task's work is class SuperTask extends TimerTask { private int counter = 1; public void run() { if (counter <= 3 ){ //This is where you do the work, just like Thread System.out.println("This is me, the task, doing my     work " + " time number " + counter + "!"); counter++; } else { System.out.println("All done."); //put this thread to rest timer.cancel(); } } } //end SuperTask class } //end StoppableTask class 

Here is the output if everything falls into place:

 

 Starting task This is me, the task, doing my work time number 1! This is me, the task, doing my work time number 2! This is me, the task, doing my work time number 3! All done. 

I suggest you buy as many blues albums as you can. Start with Robert Johnson, Skip Jones, and Muddy Waters, and work your way through Blind Willie McTell, JB Lenoir, Koko Taylor, Willie Dixon, and Keb Mo'.

Creating a TimerTask as a Daemon

This is easy and fun and it has a purpose. My favorite kind of thing. If you need to run a task, and then stop it after it has run, you need to stop it manually as we have seen. Except in one case: If you create the timer as a daemon, your task's thread will stop automagically when it realizes that there are only daemon threads left (and no user-invoked threads).

Here's how we do it:

 

 new Timer(true); 

I told you it was easy.

Why would you want to schedule a thread as a daemon thread? Well, think of a background process. If you are going to need this task to schedule repeated maintenance of the app, that you need to do throughout the application's life, this is a good choice. Think "garbage collector."



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net