/****************************************************************** * @(#) Threads.java 1.0 * * Copyright (c) 2003 John Miller * All Right Reserved *----------------------------------------------------------------- * Permission to use, copy, modify and distribute this software and * its documentation without fee is hereby granted provided that * this copyright notice appears in all copies. * WE MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. WE SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY ANY USER AS A RESULT OF USING, * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. *----------------------------------------------------------------- * * @version 1.0, 30 Mar 2003 * @author John Miller */ import static java.lang.System.*; /****************************************************************** * This class illustartes simple thread synchronization. */ public class Threads { /************************************************************** * Inner class for Thread1. */ public class Thread1 extends Thread { public void run () { out.println ("Thread 1 in stage 1"); thread2.start (); synchronized (this) { try { wait (); } catch (InterruptedException ex) {} } // synchronized out.println ("Thread 1 in stage 2"); // thread2.interrupt (); synchronized (thread2) { thread2.notify (); } } // run } // Thread1 inner class /************************************************************** * Inner class for Thread2. */ public class Thread2 extends Thread { public void run () { out.println ("Thread 2 in stage 1"); // thread1.interrupt (); synchronized (thread1) { thread1.notify (); } synchronized (this) { try { wait (); } catch (InterruptedException ex) {} } // synchronized out.println ("Thread 2 in stage 2"); } // run } // Thread2 inner class private Thread thread1 = new Thread1 (); private Thread thread2 = new Thread2 (); /************************************************************** * Main method for running Threads application. * @param args the command-line arguments */ public static void main (String [] args) { out.println ("Threads trace begin"); Threads test = new Threads (); test.thread1.start (); } // main } // Threads class