UseThreadsImproved.java
// Improved MyThread. class MyThread implements Runnable { Thread thrd; // Construct a new thread. MyThread(String name) { thrd = new Thread(this, name); thrd.start(); // start the thread } // Begin execution of new thread. public void run() { System.out.println(thrd.getName() + " starting."); try { for(int count=0; count < 10; count++) { Thread.sleep(400); System.out.println("In " + thrd.getName() + ", count is " + count); } } catch(InterruptedException exc) { System.out.println(thrd.getName() + " interrupted."); } System.out.println(thrd.getName() + " terminating."); } } class UseThreadsImproved { public static void main(String[] args) { System.out.println("Main thread starting."); MyThread mt = new MyThread("Child #1"); for(int i=0; i < 50; i++) { System.out.print("."); try { Thread.sleep(100); } catch(InterruptedException exc) { System.out.println("Main thread interrupted."); } } System.out.println("Main thread ending."); } }
C:\ece538\java_thread>java UseThreadsImproved Main thread starting. .Child #1 starting. ...In Child #1, count is 0 ....In Child #1, count is 1 ....In Child #1, count is 2 ....In Child #1, count is 3 ....In Child #1, count is 4 ....In Child #1, count is 5 ....In Child #1, count is 6 ....In Child #1, count is 7 ....In Child #1, count is 8 ....In Child #1, count is 9 Child #1 terminating. ..........Main thread ending.
Maintained by John Loomis, updated Sat Nov 17 18:26:27 2012