• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cancelling Timer and TimerTasks

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All:

I have a number of files running a thread and starting three other threads using Timer and TimerTask. I'm running on UNIX System Services, which is IBM's UNIX on z/OS. I'm running Java SE 1.5. My problem is that at least one thread does not end causing the main task to remain active. The following code is for my test versions.

//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

This is the output.

+-----------------------------------------+
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.

At least one thread does not end and I must cancel my TSO session. What am I doing wrong?

Thanks...Walter
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Next time you post code, please put it into Code tags (you can use the [ Code ] button at the top of the post form).

Thread 1 is the thread that never ends. Here is the formatted code for Thread1's run() method:


Inside your while(true) loop, you have the try/catch, and catch the InterruptedException. You handle the exception by canceling all the other Threads. You then debug with "Exited run method." But that statement is false. When you catch the InterruptedException you are handling the error. By the end of the catch block, the exception has been 'handled' so execution can continue - back into the while(true) loop where it can wait forever!

A better scenario would be something like this:
 
Walter Falby
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steve:

Thanks for the suggestion. It works! Next time I'll post code correctly.

Walter
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic