2
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
감사합니다. 그러나 동기화 된 메서드는 스레드가 이미 있으면 모든 스레드가 스레드를 입력 할 수 없게한다고 생각했습니다. 이 개념을 이끌어 낸이 개념에 대해 내가 잘못 이해 한 것일까 요? – 3ashmawy
@ 3ashmawy : 예,하지만 스레드가'wait()'를 호출하면 잠금을 해제합니다. 이후의'notifyAll()'대기 상태의 쓰레드가 대기 상태가되어 잠금을 다시 얻고'wait()'후에 계속된다. 분명히 대기열의 첫 번째 스레드는 상태를 변경하고 마지막 스레드는 다시 충족되지 않는 조건을 찾습니다. – axtavt