2
我在刷新我關於Java併發的記憶,並且在流行的生產者消費者問題上大顯身手。如果有一個生產者和一個消費者,我已經實現了下面的代碼,它能夠正常工作。但是,如果有多個生產者/消費者,它不能正確運行。我不明白爲什麼Java中的多個生產者和消費者問題(無BlockingQueue)
public class ProducerConsumer {
static Monitor monitor;
public ProducerConsumer(int maxSize)
{
monitor = new Monitor(maxSize);
new Producer().start();
new Producer().start();
new Consumer().start();
new Consumer().start();
}
class Producer extends Thread{
@Override
public void run() {
while(true)
{
try {
monitor.insert();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer extends Thread{
@Override
public void run() {
while(true)
{
try {
monitor.remove();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Monitor {
int n;
int maxSize;
public Monitor(int maxSize)
{
n=0;
this.maxSize = maxSize;
}
synchronized void insert() throws InterruptedException
{
if(n==maxSize)
wait();
System.out.println("Producer: "+n++);
if(n==1)
notifyAll();
}
synchronized void remove() throws InterruptedException
{
if(n==0)
wait();
System.out.println("Consumer: "+n--);
if(n==maxSize-1)
notifyAll();
}
}
public static void main(String[] args) {
ProducerConsumer pc = new ProducerConsumer(100);
}
}
我第二(墊+ 1) – ant 2011-03-02 14:54:57
謝謝你,工作。但是,我認爲synchronized方法不允許任何線程輸入它,如果一個線程已經在它。我對導致這種行爲的這個概念有什麼誤解? – 3ashmawy 2011-03-02 15:03:37
@ 3ashmawy:是的,但是當線程調用wait()時,它釋放鎖。之後'notifyAll()'被喚醒的線程排隊後再次獲得鎖,並在wait()後繼續。顯然,該隊列中的第一個線程會改變狀態,最後一個線程會發現條件不再被滿足。 – axtavt 2011-03-02 15:10:26