请问,如果我要再多加一个消费者,Bprocessed该如何做呢?

package buffer;
public class BufferLock //加互斥?的???
{
private int value; //共享?量
private boolean isEmpty = true; //value是否?空的信?量

public synchronized void put(int i) //同步方法
{
while (!isEmpty) //?value不空?,等待
{
try
{
this.wait(); //使?用?方法的?前?程等待,即阻塞自己
}
catch(InterruptedException e) {}
}

value = i; //?value空?,value?得值
isEmpty = false; //?置value?不空??
notify(); //?醒其他等待?程
}

public synchronized int get() //同步方法
{
while (isEmpty) //?value空?,等待
{
try
{
this.wait();
}
catch(InterruptedException e) {}
}

isEmpty = true; //?置value?空??,并返回值
notify();
return value;
}
}

class Pproduced extends Thread //?送?程?
{
private BufferLock buffer;

public Pproduced(BufferLock buffer)
{
this.buffer = buffer;
}

public void run( )
{
for (int i=1;i<20;i++)
{
buffer.put( i );
System.out.println("P produced : " + i );
}
}
}

class Aprocessed extends Thread //接收?程?
{
private BufferLock buffer;

public Aprocessed(BufferLock buffer)
{
this.buffer = buffer ;
}

public void run()
{
for (int i=1; i<20;i++)
System.out.println("\t\t\t A processed : " + buffer.get()) ;
}

public static void main (String args[])
{
BufferLock buffer = new BufferLock();
(new Pproduced(buffer)).start();
(new Aprocessed(buffer)).start();

}
谁知道当Pproduced ,去到 Pproduced 5的时候,可以叫 processed 先消费吗,实际上没看过有生产者生产超过5个以上,消费者还没消费
请问,我要再多加一个消费者,Bprocessed 如何做呢,加了有bug...希望完整解答谢谢.
顺序的消费
P produced : 1
A processed : 1
P produced : 2 B processed 2
P produced : 3 A processed : 3
P produced : 4 B processed 4

动漫人物
浏览 48回答 1
1回答

繁花如伊

package com.wormwood.thread;import java.util.LinkedList;public class BufferLock //加互斥?的???{private LinkedList<Integer> stack = new LinkedList<Integer>();public synchronized void put(int i) //同步方法{stack.push(new Integer(i)); //?value空?,value?得值}public synchronized int get() //同步方法{int value = 0;if(!stack.isEmpty()){value = stack.removeFirst().intValue();}return value;}public static void main (String args[]){BufferLock buffer = new BufferLock();(new Pproduced(buffer)).start();(new Aprocessed(buffer)).start();}}class Pproduced extends Thread //?送?程?{private BufferLock buffer;public Pproduced(BufferLock buffer){this.buffer = buffer;}public void run( ){for (int i=1;i<20;i++){buffer.put( i );System.out.println("P produced : " + i );}}}class Aprocessed extends Thread //接收?程?{private BufferLock buffer;public Aprocessed(BufferLock buffer){this.buffer = buffer ;}public void run(){for (int i=1; i<20;i++)System.out.println("\t\t\t A processed : " + buffer.get()) ;}}运行情况:P produced : 1P produced : 2P produced : 3P produced : 4P produced : 5P produced : 6A processed : 1A processed : 7A processed : 6P produced : 7A processed : 5P produced : 8A processed : 8P produced : 9A processed : 9P produced : 10P produced : 11P produced : 12A processed : 10P produced : 13A processed : 13P produced : 14A processed : 14P produced : 15A processed : 15P produced : 16A processed : 16P produced : 17A processed : 17P produced : 18A processed : 18P produced : 19A processed : 19A processed : 12A processed : 11A processed : 4A processed : 3A processed : 2分析:这锁好像没什么用[ A processed : 7]还没有生产出来就消费了,也试过:List list = Collections.synchronizedList(new LinkedList(...));也有出现还没有生产出来就消费了....楼主的程序哪个private int value; //共享?量private boolean isEmpty = true; //value是否?空的信?量看意思是哪样,一般不会这样用的......道理很简单:生产出来个的商品不可能只放在一个变量里,应该是集合
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java