生产者和消费者问题是很常见的一类多线程问题。问题大致描述为:
有一个生产者在生产产品,这些产品将提供给若干个消费者去消费,为了使生产者和消费者能并发执行,在两者之间设置一个具有多个缓冲区的缓冲池,生产者将它生产的产品放入一个缓冲区中,消费者可以从缓冲区中取走产品进行消费,显然生产者和消费者之间必须保持同步,即不允许消费者到一个空的缓冲区中取产品,也不允许生产者向一个已经放入产品的缓冲区中再次投放产品。当遇到多个生产者和消费者时候可能会出现一些问题。下面代码分析:
//创建资源类
class Resource
{
private String name;
private int count = 1;
private boolean flag = false;
public synchronized void set(String name)
{
if(flag)
try{wait();}catch(Exception e){}
this.name = name+"------"+count++;
System.out.println(Thread.currentThread().getName()+"--生产者-"+this.name);
flag = true;
this.notify();
}
public synchronized void out()
{
if(!flag)
try{wait();}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"--------消费者--------"+this.name);
flag = false;
this.notify();
}
}
//创建生产者类
class Producer implements Runnable
{
private Resource res;
Producer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
res.set("+商品+");
}
}
}
//创建消费者类
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
res.out();
}
}
}
//测试类
class ProducerConsunmerDemo
{
public static void main(String[] args)
{
Resource r = new Resource();
Producer pro = new Producer(r);
Consumer con = new Consumer(r);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread t3 = new Thread(con);
Thread t4 = new Thread(con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
看到这里发现,生产者生产了两次,消费者才开始消费。
下面我们来分析一下:
t1,t2为生产者,t3,t4为消费者。当程序运行的时候:
一开始因为flag为false,所以生产者t1进行生产,然后flag = true;
这时t1又一次获取了cpu的执行权,因为此时flag=true,所以t1被wait了。
这时候t2获取了cpu的执行权,t2进来了,发现flag=true,也挂起来了。
这是t3获取了cpu的执行权,进来了输出。然后flag=false.然后唤醒了t1;
这时t3进来了,被挂起来了。t4进来也被挂起来了。t1这时候输出以后唤醒的是线程池中的t2,t2继续往下运行,不会判断flag的标志。
所以我们希望每次都可以判断一下flag,所以:
这是发现:
运行停止了这是因为都处于等待了;因为前面我们唤醒的有可能都是生产者或者都是消费者。
于是正常了:
总结:
1、对于多个生产者与消费者,为什么要用while判断标记?
因为要让被唤醒的线程再一次判断标记。
2、为什么定义notifyAll?
因为需要唤醒对方线程;因为只用notify却只能唤醒本方线程的情况。导致程序中的所有线程都等待。
下面看看JDK1.5以后的多线程升级解决方案。
将同步Synchronized替换为显示的Lock操作。将Object中的wait,notify, notifyAll替换了Condition对象。该对象可以通过Lock锁进行获取。该实例中,实现了本方只唤醒对方的操作。
class Resource
{
private String name;
private int count = 1;
private boolean flag = false;
private Lock lock = new ReentrantLock();
private Condition condition_pro = lock.newCondition();
private Condition condition_con = lock.newCondition();
public void set(String name)throws InterruptedException
{
lock.lock();
try
{
while(flag)
condition_pro.await();
this.name = name+"------"+count++;
System.out.println(Thread.currentThread().getName()+"--生产者-"+this.name);
flag = true;
condition_con.signal();
}
finally
{
lock.unlock(); //释放锁的动作一定要执行
}
}
public void out() throws InterruptedException
{
try
{
lock.lock();
while(!flag)
condition_con.await();
System.out.println(Thread.currentThread().getName()+"--------消费者--------"+this.name);
flag = false;
condition_pro.signal();
}
finally
{
lock.unlock();
}
}
}
class Producer implements Runnable
{
private Resource res;
Producer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try
{
res.set("+商品+");
}
catch(InterruptedException e)
{
}
}
}
}
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
try
{
res.out();
}
catch(InterruptedException e)
{
}
}
}
}
class ProducerConsunmerDemo3
{
public static void main(String[] args)
{
Resource r = new Resource();
Producer pro = new Producer(r);
Consumer con = new Consumer(r);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread t3 = new Thread(con);
Thread t4 = new Thread(con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
这是一开始的单个Condition的情况:
这是多个Condition的情况:一个锁可以对应多个Condition.