一个JAVA源文件中最多只能有一个public class,此class的名字必须同文件名一样。
你定义了两个public class,也可以把class waitNotify 的public 去掉就行乐。

解决方案 »

  1.   

    我把代码变为:
    import java.applet.*;class Dealer
    {
    int goods=0;

    public synchronized int consume()
    {
    int temp;
    while (goods==0)
    {
    try {wait();}
    catch(interruptedException e){}
    }
    temp=goods;
    goods=0;
    System.out.println("Consumed:"+temp);
    notify();
    return temp;
    }
    public synchronized void produce(int amount)
    {
    while (goods!=0)
    { try
    {
    wait();
    }
    catch (InterruptedExceptioon e){}
    }
    goods=amount;
    notify();
    System.out.println("Produced:"+goods);
    }
    }class myThread extends Thread
    {
    boolean prouducer=false;
    Dealer dealer;
    public myThread(Dealer d,String type)
    {
    dealer=d;
    if (type.equals("producer"))
    producer=true;
    }
    public void run()
    {
    for (int i=0;i<10;i++)
    {try {sleep((int)(Math.random()*200));}
    catch (InterruptedException e){}
    if (producer)
    dealer.produce((int)(Math.random()*10)+1);
    else
    dealer.consume();
    }
    }
    }
    public class waitNotify extends Applet
    {
    Dealer dealer=new Dealer();
    public void init()
    {
    new myThread(dealer,"Consumer").start();
    new myThread(dealer,"Producer").start(); }
    }
    并且文件名改为waitNotify反倒出现更多错误:
    C:\vj\MyProjects\waitNotify\waitNotify.java(37,14) : error J0049: Undefined name 'InterruptedExceptioon'
    C:\vj\MyProjects\waitNotify\waitNotify.java(37,14) : error J0044: Cannot find definition for class 'InterruptedExceptioon'
    C:\vj\MyProjects\waitNotify\waitNotify.java(22,11) : error J0049: Undefined name 'interruptedException'
    C:\vj\MyProjects\waitNotify\waitNotify.java(22,11) : error J0044: Cannot find definition for class 'interruptedException'
    C:\vj\MyProjects\waitNotify\waitNotify.java(60,8) : error J0049: Undefined name 'producer'
    C:\vj\MyProjects\waitNotify\waitNotify.java(53,6) : error J0049: Undefined name 'producer'
    这是为什么?
    请指点
      

  2.   

    prouducer -> producer
    InterruptedExceptioon -> InterruptedException
    interruptedException -> InterruptedException