Go Back   Velocity Reviews > General Computer Discussion > Software
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread
Old 04-29-2009, 09:08 PM   #1
Default Cancelling Timer and TimerTask threads


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
wfalby is offline   Reply With Quote
Old 04-30-2009, 01:04 PM   #2
wfalby
Junior Member
 
Join Date: Apr 2009
Posts: 2
Default
This problem has been resolved. You can put it down to not seeing the obvious.

Walter


wfalby
wfalby is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46