Cancelling Timer and TimerTask threads

Discussion in 'Software' started by wfalby, Apr 29, 2009.

  1. wfalby

    wfalby

    Joined:
    Apr 29, 2009
    Messages:
    2
    I'm running Java SE 1.5 under UNIX System Services, which is IBM's UNIX on z/OS. I have 3 files. One that starts a thread. The thread then schedules another thread to run on a particular interval. The threads runs, however the main thread does not end.

    //Test code
    import java.util.*;
    import java.lang.Thread.*;
    public class TestEveProc
    {
    public static void main(String argsݨ)
    {
    System.out.println("+-----------------------------------------+");
    System.out.println("Starting Thread1");
    Thread1 proc = new Thread1();
    proc.start();
    try
    {
    Thread.sleep(60000);
    }
    catch (InterruptedException ex)
    {
    System.out.println("Unexpected interruption exception!");
    }
    System.out.println("Stopping Thread1");
    proc.interrupt();
    System.out.println("Stopped Thread1");
    }
    }

    /**
    * Thread1.java
    *
    */
    import org.apache.log4j.Logger;
    import java.util.Timer;
    import java.util.TimerTask;

    public class Thread1 extends Thread
    {
    static Logger logger = Logger.getLogger("Thread1");
    private TimerTask thread2;
    private TimerTask thread3;
    private TimerTask thread4;

    public Thread1()
    {
    logger.debug("Entered Thread1 constructor.");

    logger.debug("Exited Thread1 constructor.");
    }
    /**
    * Start the threads.
    */
    public void run()
    {
    logger.debug("Entered run method.");

    thread2 = new Thread2();
    thread3 = new Thread2();
    thread4 = new Thread2();
    Timer timer = new Timer(true);
    timer.scheduleAtFixedRate(thread2, 5000, 10000);
    timer.scheduleAtFixedRate(thread3, 5000, 10000);
    timer.scheduleAtFixedRate(thread4, 5000, 10000);

    while (true)
    {
    try
    {
    Thread.sleep(5000);
    }
    catch (InterruptedException nex)
    {
    boolean bFlg = thread2.cancel();
    System.out.println("Result of thread2 cancel = " + bFlg);
    bFlg = thread3.cancel();
    System.out.println("Result of thread3 cancel = " + bFlg);
    bFlg = thread4.cancel();
    System.out.println("Result of thread4 cancel = " + bFlg);

    timer.cancel();
    System.out.println("Exited run method.");
    logger.debug("Exited run method.");
    }
    }
    }

    } // end class Thread1

    /**
    * Thread2.java
    *
    */
    import org.apache.log4j.Logger;
    import java.text.DateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.TimerTask;

    public class Thread2 extends TimerTask
    {
    static Logger logger = Logger.getLogger("Thread2");

    /**
    * Initial setup
    */
    public Thread2()
    {
    logger.debug("Entered Thread2 constructor.");

    logger.debug("Exited Thread2 constructor.");
    }
    /**
    * Add special event ID to event ID queue.
    */
    public void run()
    {
    logger.debug("Entered Thread2 run method.");

    try
    {
    DateFormat dateFormat = DateFormat.getDateTimeInstance(
    DateFormat.LONG, DateFormat.LONG);
    String date = dateFormat.format(new Date(Calendar.getInstance().getTimeInMillis()));
    System.out.println("Entered run method at time: " + date);
    Thread.sleep(2000);
    }
    catch (InterruptedException ex) {;}

    logger.debug("Exited Thread2 run method.");
    }

    } // end class Thread2

    +-----------------------------------------+
    Starting Thread1
    Entered run method at time: April 29, 2009 3:39:04 PM EDT
    Entered run method at time: April 29, 2009 3:39:06 PM EDT
    Entered run method at time: April 29, 2009 3:39:08 PM EDT
    Entered run method at time: April 29, 2009 3:39:14 PM EDT
    Entered run method at time: April 29, 2009 3:39:16 PM EDT
    Entered run method at time: April 29, 2009 3:39:18 PM EDT
    Entered run method at time: April 29, 2009 3:39:24 PM EDT
    Entered run method at time: April 29, 2009 3:39:26 PM EDT
    Entered run method at time: April 29, 2009 3:39:28 PM EDT
    Entered run method at time: April 29, 2009 3:39:34 PM EDT
    Entered run method at time: April 29, 2009 3:39:36 PM EDT
    Entered run method at time: April 29, 2009 3:39:38 PM EDT
    Entered run method at time: April 29, 2009 3:39:44 PM EDT
    Entered run method at time: April 29, 2009 3:39:46 PM EDT
    Entered run method at time: April 29, 2009 3:39:48 PM EDT
    Entered run method at time: April 29, 2009 3:39:54 PM EDT
    Entered run method at time: April 29, 2009 3:39:56 PM EDT
    Entered run method at time: April 29, 2009 3:39:58 PM EDT
    Stopping Thread1
    Stopped Thread1
    Result of thread2 cancel = true
    Result of thread3 cancel = true
    Result of thread4 cancel = true
    Exited run method.

    Walter
     
    wfalby, Apr 29, 2009
    #1
    1. Advertising

  2. wfalby

    wfalby

    Joined:
    Apr 29, 2009
    Messages:
    2
    This problem has been resolved. You can put it down to not seeing the obvious.

    Walter
     
    wfalby, Apr 30, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Perk
    Replies:
    1
    Views:
    607
    ::..BRIAN..::
    Jan 1, 2005
  2. The Invisible Man

    TB View, Threads, Threads with unread

    The Invisible Man, Mar 6, 2006, in forum: Firefox
    Replies:
    1
    Views:
    649
    Splibbilla
    Mar 20, 2006
  3. Brian H¹©

    Cancelling posting

    Brian H¹©, Jun 30, 2003, in forum: Computer Support
    Replies:
    8
    Views:
    541
    trout
    Jun 30, 2003
  4. faceinthemoon

    Seeing Windows wallpaper when cancelling the screensaver

    faceinthemoon, Jul 23, 2003, in forum: Computer Support
    Replies:
    0
    Views:
    435
    faceinthemoon
    Jul 23, 2003
  5. Mal Carter
    Replies:
    3
    Views:
    787
    VanguardLH
    Jan 12, 2008
Loading...

Share This Page