listing 1 // Create a thread by implementing Runnable. class MyThread implements Runnable { int count; String thrdName; MyThread(String name) { count = 0; thrdName = name; } // Entry point of thread. public void run() { System.out.println(thrdName + " starting."); try { do { Thread.sleep(500); System.out.println("In " + thrdName + ", count is " + count); count++; } while(count < 10); } catch(InterruptedException exc) { System.out.println(thrdName + " interrupted."); } System.out.println(thrdName + " terminating."); } } class UseThreads { public static void main(String args[]) { System.out.println("Main thread starting."); // First, construct a MyThread object. MyThread mt = new MyThread("Child #1"); // Next, construct a thread from that object. Thread newThrd = new Thread(mt); // Finally, start execution of the thread. newThrd.start(); do { System.out.print("."); try { Thread.sleep(100); } catch(InterruptedException exc) { System.out.println("Main thread interrupted."); } } while (mt.count != 10); System.out.println("Main thread ending."); } } listing 2 // Improved MyThread. class MyThread implements Runnable { int count; Thread thrd; // Construct a new thread. MyThread(String name) { thrd = new Thread(this, name); count = 0; thrd.start(); // start the thread } // Begin execution of new thread. public void run() { System.out.println(thrd.getName() + " starting."); try { do { Thread.sleep(500); System.out.println("In " + thrd.getName() + ", count is " + count); count++; } while(count < 10); } 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"); do { System.out.print("."); try { Thread.sleep(100); } catch(InterruptedException exc) { System.out.println("Main thread interrupted."); } } while (mt.count != 10); System.out.println("Main thread ending."); } } listing 3 /* Project 11-1 Extend Thread. */ class MyThread extends Thread { int count; // Construct a new thread. MyThread(String name) { super(name); // name thread count = 0; start(); // start the thread } // Begin execution of new thread. public void run() { System.out.println(getName() + " starting."); try { do { Thread.sleep(500); System.out.println("In " + getName() + ", count is " + count); count++; } while(count < 10); } catch(InterruptedException exc) { System.out.println(getName() + " interrupted."); } System.out.println(getName() + " terminating."); } } class ExtendThread { public static void main(String args[]) { System.out.println("Main thread starting."); MyThread mt = new MyThread("Child #1"); do { System.out.print("."); try { Thread.sleep(100); } catch(InterruptedException exc) { System.out.println("Main thread interrupted."); } } while (mt.count != 10); System.out.println("Main thread ending."); } } listing 4 // Create multiple threads. class MyThread implements Runnable { int count; Thread thrd; // Construct a new thread. MyThread(String name) { thrd = new Thread(this, name); count = 0; thrd.start(); // start the thread } // Begin execution of new thread. public void run() { System.out.println(thrd.getName() + " starting."); try { do { Thread.sleep(500); System.out.println("In " + thrd.getName() + ", count is " + count); count++; } while(count < 10); } catch(InterruptedException exc) { System.out.println(thrd.getName() + " interrupted."); } System.out.println(thrd.getName() + " terminating."); } } class MoreThreads { public static void main(String args[]) { System.out.println("Main thread starting."); MyThread mt1 = new MyThread("Child #1"); MyThread mt2 = new MyThread("Child #2"); MyThread mt3 = new MyThread("Child #3"); do { System.out.print("."); try { Thread.sleep(100); } catch(InterruptedException exc) { System.out.println("Main thread interrupted."); } } while (mt1.count < 10 || mt2.count < 10 || mt3.count < 10); System.out.println("Main thread ending."); } } listing 5 // Use isAlive(). class MoreThreads { public static void main(String args[]) { System.out.println("Main thread starting."); MyThread mt1 = new MyThread("Child #1"); MyThread mt2 = new MyThread("Child #2"); MyThread mt3 = new MyThread("Child #3"); do { System.out.print("."); try { Thread.sleep(100); } catch(InterruptedException exc) { System.out.println("Main thread interrupted."); } } while (mt1.thrd.isAlive() || mt2.thrd.isAlive() || mt3.thrd.isAlive()); System.out.println("Main thread ending."); } } listing 6 // Use join(). class MyThread implements Runnable { int count; Thread thrd; // Construct a new thread. MyThread(String name) { thrd = new Thread(this, name); count = 0; thrd.start(); // start the thread } // Begin execution of new thread. public void run() { System.out.println(thrd.getName() + " starting."); try { do { Thread.sleep(500); System.out.println("In " + thrd.getName() + ", count is " + count); count++; } while(count < 10); } catch(InterruptedException exc) { System.out.println(thrd.getName() + " interrupted."); } System.out.println(thrd.getName() + " terminating."); } } class JoinThreads { public static void main(String args[]) { System.out.println("Main thread starting."); MyThread mt1 = new MyThread("Child #1"); MyThread mt2 = new MyThread("Child #2"); MyThread mt3 = new MyThread("Child #3"); try { mt1.thrd.join(); System.out.println("Child #1 joined."); mt2.thrd.join(); System.out.println("Child #2 joined."); mt3.thrd.join(); System.out.println("Child #3 joined."); } catch(InterruptedException exc) { System.out.println("Main thread interrupted."); } System.out.println("Main thread ending."); } } listing 7 // Demonstrate thread priorities. class Priority implements Runnable { int count; Thread thrd; static boolean stop = false; static String currentName; /* Construct a new thread. Notice that this constructor does not actually start the threads running. */ Priority(String name) { thrd = new Thread(this, name); count = 0; currentName = name; } // Begin execution of new thread. public void run() { System.out.println(thrd.getName() + " starting."); do { count++; if(currentName != thrd.getName()) { currentName = thrd.getName(); System.out.println("In " + currentName); } } while(stop == false && count < 1000); stop = true; System.out.println("\n" + thrd.getName() + " terminating."); } } class PriorityDemo { public static void main(String args[]) { Priority mt1 = new Priority("High Priority"); Priority mt2 = new Priority("Low Priority"); // set the priorities mt1.thrd.setPriority(Thread.NORM_PRIORITY+2); mt2.thrd.setPriority(Thread.NORM_PRIORITY-2); // start the threads mt1.thrd.start(); mt2.thrd.start(); try { mt1.thrd.join(); mt2.thrd.join(); } catch(InterruptedException exc) { System.out.println("Main thread interrupted."); } System.out.println("\nHigh priority thread counted to " + mt1.count); System.out.println("Low priority thread counted to " + mt2.count); } } listing 8 // Use synchronize to control access. class SumArray { private int sum; synchronized int sumArray(int nums[]) { sum = 0; // reset sum for(int i=0; i