需求:定义两个线程,一个负责生产,一个负责消费
class Resource
{
private String name;
private int count=1;
boolean flag=false;
public synchronized void setName(String name)
{
if (flag)
try{this.wait();}catch (Exception e){System.out.println(e.toString());}
this.name=name+"---"+count++;
System.out.println("生产"+this.name);
flag=true;
this.notify();
}
public synchronized void out()
{
if (!flag)
try{this.wait();}catch (Exception e){System.out.println(e.toString());}
System.out.println("消费"+this.name);
flag=false;
this.notify();
}
}
class Produce implements Runnable
{
private Resource r;
Produce(Resource r)
{
this.r=r;
}
public void run()
{
while (true)
{
r.setName("商品");
}
}
}
class Custom implements Runnable
{
private Resource r;
Custom(Resource r)
{
this.r=r;
}
public void run()
{
while (true)
{
r.out();
}
}
}
class DeadLock
{
public static void main(String[] args)
{
Resource r=new Resource();
Produce p=new Produce(r);
Custom c=new Custom(r);
Thread t1=new Thread(p);
Thread t2=new Thread(c);
t1.start();
t2.start();
}
}
代码像上面那样写能正常运行,生产一个输出一个。注意同步函数的锁是this,所以wait()和notify()前面要加上this。
但是如果定义多个线程就会出现问题,如两个线程负责生产,两个线程负责消费就会出现问题
看下面代码:
class Resource
{
private String name;
private int count=1;
boolean flag=false;
public synchronized void setName(String name)
{
if (flag)
try{this.wait();}catch (Exception e){System.out.println(e.toString());}
this.name=name+"---"+count++;
System.out.println(Thread.currentThread()+"生产"+this.name);
flag=true;
this.notify();
}
public synchronized void out()
{
if (!flag)
try{this.wait();}catch (Exception e){System.out.println(e.toString());}
System.out.println(Thread.currentThread()+"消费------"+this.name);
flag=false;
this.notify();
}
}
class Produce implements Runnable
{
private Resource r;
Produce(Resource r)
{
this.r=r;
}
public void run()
{
while (true)
{
r.setName("商品");
}
}
}
class Custom implements Runnable
{
private Resource r;
Custom(Resource r)
{
this.r=r;
}
public void run()
{
while (true)
{
r.out();
}
}
}
class DeadLock
{
public static void main(String[] args)
{
Resource r=new Resource();
Produce p=new Produce(r);
Custom c=new Custom(r);
Thread t1=new Thread(p);
Thread t2=new Thread(p);
Thread t3=new Thread(c);
Thread t4=new Thread(c);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
上面代码的打印结果为:
Thread[Thread-1,5,main]生产商品---1390
Thread[Thread-0,5,main]生产商品---1391
Thread[Thread-2,5,main]消费------商品---1391
Thread[Thread-1,5,main]生产商品---1392
Thread[Thread-0,5,main]生产商品---1393
Thread[Thread-2,5,main]消费------商品---1393
Thread[Thread-1,5,main]生产商品---1394
Thread[Thread-0,5,main]生产商品---1395
Thread[Thread-2,5,main]消费------商品---1395
Thread[Thread-1,5,main]生产商品---1396
Thread[Thread-0,5,main]生产商品---1397
Thread[Thread-2,5,main]消费------商品---1397
Thread[Thread-1,5,main]生产商品---1398
Thread[Thread-0,5,main]生产商品---1399
Thread[Thread-2,5,main]消费------商品---1399
Thread[Thread-1,5,main]生产商品---1400
Thread[Thread-0,5,main]生产商品---1401
Thread[Thread-2,5,main]消费------商品---1401
Thread[Thread-1,5,main]生产商品---1402
Thread[Thread-0,5,main]生产商品---1403
Thread[Thread-2,5,main]消费------商品---1403
也就是生产了两次却消费了一次
造成该种现象的原因可概括为:原因:t1先抢到执行权,判断falg为假,执行生产打印出"生产6786",然后将flag置为true,t2进行判断flag为true则停在wait()处放弃资格。t3获取执行权执行一次消费打印出“消费6786”,这是正常模式。然后t3将flag置为false,t4进行判断flag为假则停在wait()处。这个时候t1有可能再次抢到执行权,判断flag为假,直接进行生产,生产后直接唤醒所有线程,因为t2是最先在线程池中等待的线程,所以将会第一个被唤醒,t2被唤醒后将不会去判断flag条件,而是直接进行生产,这就是造成生产两次的原因。
改进方法:将 if(!flag)改为while(!flag),将if(flag)改为while(flag)。