这个例子主线程从控制台输入一行文本,子线程输出。
import java.io.*;
import java.util.*;public class NotifyTest extends Thread
{
    public NotifyTest()
    {
        this.setName("Output Thread");
        this.setDaemon(true);
    }
    public static void main(String[] args)
    {
        try
        {
            NotifyTest x=new NotifyTest();
            x.start();
            BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
            for(; ; )
            {
                String line=in.readLine();
                x.notifyMessage(line);
            }
        }
        catch(Throwable e)
        {
            e.printStackTrace();
        }
    }
    private LinkedList msgs = new LinkedList();
    public synchronized void run()
    {
        try
        {
            for(;;)
            {
                while(msgs.size() > 0)
                {
                    System.out.println("[" + Thread.currentThread().getName() + "]: " + msgs.removeLast());
                }
                this.wait();
            }
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }
    }
    public synchronized void notifyMessage(String msg)
    {
        msgs.addLast(msg);
        this.notify();
    }
}

解决方案 »

  1.   

    把注解打开会有不一样的结果
    //main program
    public class ProducerAndConsumerSolution
    {
    public static void main(String args[])
    {
    SyncStack stack = new SyncStack();
    Runnable p = new Producer(stack);
    Runnable c = new Consumer(stack);
    Thread t1 = new Thread(p);
    Thread t2 = new Thread(c);
    t1.start();
    t2.start();
    }
    }//stack
    class SyncStack
    {
         int idx=0;
         char[] data = new char[6];
         public /*synchronized*/ void push(char c)
         {
    //      while(idx==data.length)
    //      {
    //      try 
    //      {
    //   this.wait();
    // }
    // catch (Exception ex) 
    // {
    // }
    //     }
    //
    // this.notify();
            data[idx] = c;
            idx++;
            System.out.println ("procdure : " + c);     }
     public /*synchronized*/ char pop()
     {
    //   while(idx==0)
    //   {
    //   try 
    //   {
    // this.wait();
    // }
    // catch (Exception ex) 
    // {
    // }
    //   }
    //   this.notify();
            idx--;
            System.out.println ("consumer : " + data[idx]);
            return data[idx];
          }
    }//Producer
    class  Producer implements Runnable
    {
    SyncStack stack;
    public Producer(SyncStack s)
    {
    stack = s;
    }
    public void run()
    {
    for(int i=0; i<20; i++)
    {
    char c =(char)(Math.random()*26+'A');
    stack.push(c);
    try
    {
    Thread.sleep((int)(Math.random()*100));
    }
    catch(InterruptedException e)
    {
    }
    }
    }
    }//Consumer
    class Consumer implements Runnable
    {
    SyncStack stack;
    public Consumer(SyncStack s)
    {
    stack = s;
    }
    public void run()
    {
    for(int i=0;i<20;i++)
    {
    char c = stack.pop();
    try
    {
    Thread.sleep((int)(Math.random()*1000));
    }
    catch(InterruptedException e){}
    }
    }
    }