Thread_Wait.java 903 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Test basic thread creation and wait/notify functionality.
// Origin: Bryce McKinlay <bryce@albatross.co.nz>

public class Thread_Wait implements Runnable
{
  public static void main(String args[])
  {
    new Thread_Wait();
  }
  
  public Thread_Wait()
  {
    System.out.println("creating thread");
    Thread t = new Thread(this);
    t.start();
    
    try
    {
      Thread.sleep(100);
    }
    catch (Exception x)
    {
23
      System.out.println("exception occurred: " + x);
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    }

    synchronized (this)
    {
      System.out.println("notifying other thread");
      notify();
    }
  }
  
  public void run()
  {
    System.out.println ("new thread running");
    synchronized (this)
    {
      try
      {
	wait();
      }
      catch (Exception x)
      {
44
        System.out.println("exception occurred: " + x);
45 46 47 48 49
      }
    }    
    System.out.println ("thread notified okay");
  }
}