작은 응용 프로그램 ("HelloWorld"보다 약간 큰 것)을 쓰고 있습니다. 작업은 단일 제작자와 여러 소비자를 만드는 것입니다. 생산자는 integeres를 생성하여 동적 스택 (이는 내 모니터 객체 임)에 넣고 소비자는 그 정수를 스택에서 가져 오려고합니다. 플래그를 사용하여 1 생산자 + 1 소비자를 생성하는 방법을 알고 있습니다. 그러나 여러 소비자와 동일한 방법. 자바 스레드를 이용한 다중 모니터 동기화
은 내 코드에서 참조하시기 바랍니다 :public interface StackQueueIF {
void push(int value);
int pop();
}
public class DynamicStack implements StackQueueIF{
private volatile int stck[];
private volatile int tos;
boolean valueSet = false;
public DynamicStack(int size) {
stck = new int[size];
tos = -1;
}
@Override
public synchronized void push(int item) {
while(valueSet){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (tos == stck.length-1) {
System.out.println("---------STACK HAS BEEN DOUBLED--------");
int temp[] = new int[stck.length * 2];
for (int i = 0; i < stck.length; i++) temp[i] = stck[i];
stck = temp;
stck[++tos] = item;
}else
stck[++tos] = item;
valueSet = true;
notifyAll();
}
@Override
public synchronized int pop() {
while(!valueSet){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
valueSet = false;
notifyAll();
return stck[tos--];
}
}
class Producer implements Runnable{
StackQueueIF queue;
String producerName;
private static int counter = 0;
public Producer(StackQueueIF queue, String name) {
this.queue = queue;
this.producerName = name;
new Thread(this,producerName).start();
}
@Override
public void run() {
while(true){
System.out.println(this.producerName + " puts " + (++counter)
+ " into stack");
this.queue.push(counter);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.err.println(producerName + " interrupted");
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable{
StackQueueIF queue;
String consumerName;
public Consumer(StackQueueIF queue, String consumerName) {
this.queue = queue;
this.consumerName = consumerName;
new Thread(this, this.consumerName).start();
}
@Override
public void run() {
while(true){
System.out.println(this.consumerName + " gets " + queue.pop() + " from stack");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.err.println(consumerName + "interrupted");
e.printStackTrace();
}
}
}
}
public class QueueTester {
public static void main(String[] args) {
StackQueueIF queue = new DynamicStack(10);
new Producer(queue,"Producer №1");
new Consumer(queue,"Consumer №1");
new Consumer(queue,"Consumer №2");
}
}
내가 스택을 작성하는 방법이다 스택에서부터 소비자 # 2는 전혀 얻지 못합니다. 그것이 문제이다. 그들은 스택에서 무언가를 얻거나 비어있을 때까지 기다려야합니다. –
+1 사용 №;) –