java线程死循环,高手来,急!
朋友们,这个好像C语言里面的死循环啊 ,帮帮我!! 运行便知
package 线程;
class ham
{
static Object box1=new Object();
static Object box2=new Object();
static int totalmaterial1=10;
static int totalmaterial2=10;
static int sales11=0;
static int sales12=0;
static int sales21=0;
static int sales22=0;
static int production1=5;
static int production2=5;
}
class hmaker1 extends Thread
{
public void make() {
synchronized(ham.box1)
{
(ham.production1)++;
System.out.println("厨师"+getName()+";"+"汉堡来了(总共"+(ham.production1-ham.sales11-ham.sales21)+"个A类汉堡)");
try
{
ham.box1.notify();
}
catch(Exception e){}
}
}
public void run()
{
while(ham.production1<ham.totalmaterial1)
{
make();
try
{
sleep(3000);
}
catch(Exception e){}
}
if(ham.production1==ham.totalmaterial1)
{
System.out.println("所有的材料都用完了!");
}
}
}
class hmaker2 extends Thread
{
public void make() {
synchronized(ham.box2)
{
(ham.production2)++;
System.out.println("厨师"+getName()+";"+"汉堡来了(总共"+(ham.production2-ham.sales12-ham.sales22)+"个B类汉堡)");
try
{
ham.box2.notify();
}
catch(Exception e){}
}
}
public void run()
{
while(ham.production2<ham.totalmaterial2)
{
make();
try
{
sleep(3000);
}
catch(Exception e){}
}
if(ham.production2==ham.totalmaterial2)
{
System.out.println("所有的材料都用完了!");
}
}
}
class hassistant extends Thread
{
public void sell1()
{
if(ham.production1==(ham.sales11+ham.sales12))
{
System.out.println("营业员"+getName()+";顾客朋友们,请稍等一下,A汉堡没有了!!");
ham.sales11=0;
ham.sales12=0;
ham.production1=0;
try
{
ham.box1.wait();
}
catch(Exception e){}
}
else
{
if(ham.production1>(ham.sales11+ham.sales12))
(ham.sales21)++;
System.out.println("营业员"+getName()+":顾客好,汉堡来了,(总共卖了A类汉堡"+ham.sales11+"个),总共卖了B类汉堡"+ham.sales22+"个)");
}
}
public void sell2()
{
if(ham.production2==(ham.sales21+ham.sales22))
{
System.out.println("营业员"+getName()+";顾客朋友们,请稍等一下,B汉堡没有了!!");
ham.sales21=0;
ham.sales22=0;
ham.production2=0;
try
{
ham.box2.wait();
}
catch(Exception e){}
}
if(ham.production1>(ham.sales21+ham.sales22))
{
(ham.sales12)++;
(ham.sales22)++;
System.out.println("营业员"+getName()+":顾客好,汉堡来了,(总共卖了A类汉堡"+ham.sales12+"个),总共卖了B类汉堡"+ham.sales22+"个)");
}
}
public void run()
{
while((ham.sales12+ham.sales11)<ham.production1)
{
try
{
sleep(1000);
}
catch(Exception e){}
sell1();
}
while((ham.sales12+ham.sales22)<ham.production2)
{
try
{
sleep(2000);
}
catch(Exception e){}
sell2();
}
System.out.println("还剩A类汉堡:"+(ham.production1-ham.sales11-ham.sales21)+"个");
System.out.println("还剩B类汉堡:"+(ham.production2-ham.sales21-ham.sales22)+"个");
}
}
public class Thread10 {
public static void main(String[] args)
{
hmaker1 maker1=new hmaker1();
hmaker2 maker2=new hmaker2();
hassistant assistant1=new hassistant();
hassistant assistant2=new hassistant();
assistant1.setName("甲");
assistant2.setName("乙");
maker1.setName("甲");
maker2.setName("乙");
maker1.start();
maker2.start();
assistant1.start();
assistant2.start();
}
}