请教一个多线程问题
这面是一个多线程的程序,请问是怎样来回调用的,也就是怎样执行的-----执行顺序?哪位朋友知道的话,请指点一下,我在这里先谢了! import *;
import java.lang.*;
public class synchoronizedThread
{
public static void main(String args[])
{
/**构建Dealer对象*/
Dealer dealer=new Dealer();
/**构建和启动生产者和消费者线程*/
new testThread(dealer,"Consumer").start();
new testThread(dealer,"Producer").start();
}
}
class Dealer
{
int goods=0;
public synchronized int consume()
{
int temp;
while(goods==0);
{
try
{
/**使线程进入等待状态*/
wait();
}
catch(InterruptedExcepion e){}
}
temp=goods;
System.out.println("Consumed: "+temp);
goods--;
/**唤醒对该对象的等待进程*/
notify();
return temp;
}
public synchronized void produce(int amout)
{
while(goods!=0)
{
try
{
/**使线程进入等待状态*/
wait();
}
catch(InterruptedException e)
{
}
}
goods=amout;
/**唤醒对该对象的等待进程*/
notyfy();
System.out.println("produced: "+goods);
}
}
class testThread extends Thread
{
booean producer;
Dealer dealer;
public testThread(Dealer d,String type)
{
dealer =d;
if(type.equals("Producer"))
producer=true;
if(type.equals("Consumer"))
producer=false;
}
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();
}
}