生产者和消费者问题:假设有一个仓库,生产者生产产品放入仓库中,消费者从仓库中消费产品。生产者和消费者有多个。
class ProducerConsumerDemo
{
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();
}
}
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();
}
}
}
输出结果中可能会出现生产了多个产品但是只消费了一个的情况。这是因为生产线程醒来后没有判断flag的原因,所以要把if换成while。但是这样就会出现全部线程都等待的情况,因此还要把notify换成notifyAll。
class ProducerConsumerDemo
{
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();
}
}
class Resource
{
private String name;
private int count = 1;
private boolean flag = false;
public synchronized void set(String name)
{
<span style="color:#33ff33;">while(flag)</span>
try
{
wait();
}
catch(Exception e)
{
}
this.name = name+"---"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
flag = true;
<span style="color:#33cc00;">this.notifyAll();</span>
}
public synchronized void out()
{
<span style="color:#33cc00;">while(!flag)</span>
try
{
wait();
}
catch(Exception e)
{
}
System.out.println(Thread.currentThread().getName()+"...消费者.."+this.name);
flag = false;
<span style="color:#33cc00;">this.notifyAll();</span>
}
}
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();
}
}
}
对于多个生产者和消费者,为什么要定义while判断标记?
让被唤醒的线程再一次判断标记。
为什么定义notifyAll?
因为需要唤醒对方线程。只用notify,容易出现只唤醒本方线程的情况,导致程序中所有的线程都等待。
JDK1.5中提供了多线程升级解决方案。
将同步synchronized替换成了显式 Lock操作,Object中的wait,notify,notifyAll,替换成了 Condition对象。该对象可以对
Lock锁进行获取。
class ProducerConsumerDemo
{
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();
}
}
class Resource
{
private String name;
private int count = 1;
private boolean flag = false;
private Lock lock =new ReentrantLock();
<span style="background-color: rgb(204, 51, 204);">private Condition <span style="color:#33cc00;">condition_pro</span> = lock.newCondition();
private Condition <span style="color:#66ffff;">condition_con </span>= lock.newCondition();</span>
public void set(String name) throws InterruptedException
{
<span style="color:#6633ff;">lock.lock();</span>
try
{
while(flag)
<span style="color:#33cc00;">condition_pro.await()</span>; //消费者等待
this.name = name+"---"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
flag = true;
<span style="color:#33ffff;">condition_con.signalAll()</span>; //生产者唤醒
}
<span style="color:#33ccff;">finally</span>
{
<span style="color:#6633ff;">lock.unlock(); </span> //释放锁的动作一定要执行
}
}
public void out()throws InterruptedException
{
lock.lock();
try
{
while(!flag)
condition_con.await();
System.out.println(Thread.currentThread().getName()+"...消费者.."+this.name);
flag = false;
condition_pro.signalAll();
}
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)
{
}
}
在该实例中实现了本方只唤醒对方操作