你看这样是不是更好一些?
class Producer extends Thread
{
   private Soup soup;
   private String alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   public Producer(Soup s)
   {
     soup=s;
   }
   public void run()
   {
     char c;
     for(int i=0;i<20;i++)
     {
        c=alphabet.charAt((int)(Math.random()*26));
        soup.add(c);
        try
         {
            sleep((int)(Math.random()*1000));
         }
        catch(InterruptedException e)
         {
         }
     }
   }
}class Consumer extends Thread
{
  private Soup soup;
  public Consumer(Soup s)
  {
    soup=s;
  }
  public void run()
  {
    char c;
    for(int i=0;i<10;i++)
    {
      c=soup.eat();
      try
      {
        sleep((int)(Math.random()*1300));
      }
      catch(InterruptedException e)
      {
      }
    }
   }
}
class Soup
{
  private char buffer[]=new char[10];
  private int anext=0;
  private int enext=0;
  private int count=0;
  public synchronized char eat()
  {
   while(count==0)
   {
    try
    {
      wait();
    }
    catch(InterruptedException e)
    {
    }
   }
   char c=buffer[enext];
   enext=(enext+1)%10;
   count--;
   System.out.println("Atealetter: "+c); 
   notify();
   return(c);
  }  public synchronized void add(char c)
  {
   while(count==10)
   {
    try
    {
      wait();
    }
    catch(InterruptedException e)
    {
    }
   }
   buffer[anext]=c;
   anext=(anext+1)%10;
   count++;
   System.out.println("Added  "+c+"  tothesoup.");      
   notify();
  }
}public class testThread
{
  public static void main(String args[])
  {
  Soup s=new Soup();
  Producer p1=new Producer(s);
  Consumer c1=new Consumer(s);
  p1.start();
  c1.start();
  }
}

解决方案 »

  1.   

    不好意思,更正一下,Consumer 类里的循环次数要与Producer类的循环次数一样。
      

  2.   

    啥意思 ,我咋不明白呢,学习ing
      

  3.   

    谢谢大家帮忙,有交流才有了彼此的成长。解决:
    class Producer extends Thread {
    private Soup soup;
    private String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public Producer(Soup s) {
    soup = s;
    }
    public void run() {
    char c;
    for (int i = 0; i < 10; i++) {
    c = alphabet.charAt((int) (Math.random() * 26));
    soup.add(c);
    System.out.println("Added " + c + " tothesoup.");
    try {
    sleep((int) (Math.random() * 1000));
    } catch (InterruptedException e) {
    }
    }
    }
    }
    class Consumer extends Thread {
    private Soup soup;
    public Consumer(Soup s) {
    soup = s;
    }
    public void run() {
    char c;
    for (int i = 0; i < 10; i++) {
    c = soup.eat();
    System.out.println("Atealetter: " + c);
    try {
    sleep((int) (Math.random() * 1100));
    } catch (InterruptedException e) {
    }
    }
    }
    }
    class Soup {
    private char buffer[] = new char[10];
    private int next = 0;
    private boolean isFull = false;
    private boolean isEmpty = true;
    public synchronized char eat() {
    while (isEmpty == true) {
    try {
    wait();
    } catch (InterruptedException e) {
    }
    }
    if (next == 1) {
    isEmpty = true;
    }
    isFull = false;
    notify();
    return (buffer[--next]);
    }
    public synchronized void add(char c) {
    while (isFull == true) {
    try {
    wait();
    } catch (InterruptedException e) {
    }
    }
    if (next == 10) {
    isFull = true;
    }
    buffer[next] = c;
    next++;
    isEmpty = false;
    notify();
    }
    }
    public class Souptest {
    public static void main(String args[]) {
    Soup s = new Soup();
    Producer p1 = new Producer(s);
    Consumer c1 = new Consumer(s);
    p1.start();
    c1.start();
    }
    }