class ProducerConsumerDome
{
public static void main(String[] args)
{
Resouer r = new Resouer();
//Thread t1 = new Thread (r);
//Thread t2 = new Thread (r);
//t1.start();
//t2.start();
new Thread(new Producer(r)).start();
new Thread(new Consumer(r)).start();
}
}
class Resouer
{
private String name;
private int courrt = 4;
boolean flas = false;
public synchronized void set(String name)
{
if(flas)
try
{
wait();
}
catch (Exception e)
{
}
this.name = name+"...."+courrt++;
System.out.println(Thread.currentThread().getName()+"...生产者."+this.name);
flas = true;
this.notify();
}
public synchronized void show()
{
if(!flas)
try
{
wait();
}
catch (Exception e)
{
}
System.out.println(Thread.currentThread().getName()+"...消费者....."+this.name);
flas = false;
this.notify();
}
}
class Producer implements Runnable
{
private Resouer res;
Producer(Resouer res)
{
this.res = res;
}
public void run()
{
while (true)
{
res.set("..商品..");
}
}
}
class Consumer implements Runnable
{
private Resouer res;
Consumer(Resouer res)
{
this.res = res;
}
public void run()
{
while (true)
{
res.show();
}
}
}
如果我这样定义boolean flas =true; 对应的 if(flas)......... flas = false; 和if(!flas)...........flas= true;
那编译运行的结果为什么是这样!!其中发生了什么变换!求解???
|
|