![]() |
|
|
|
#1 |
|
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 |
|
|
|
|
|
|
#2 |
|
Junior Member
Join Date: Apr 2009
Posts: 2
|
This problem has been resolved. You can put it down to not seeing the obvious.
Walter wfalby |
|
|
|
![]() |
| Thread Tools | Search this Thread |
|
|