gpt4 book ai didi

java - 使用java线程的多监视器同步

转载 作者:行者123 更新时间:2023-11-30 11:36:11 25 4
gpt4 key购买 nike

我正在编写一个微型应用程序(比“HelloWorld”大一点)。任务是创建一个生产者和多个消费者。生产者生成整数并将它们放入动态堆栈(这是我的监视器对象)中,消费者尝试从堆栈中获取这些整数。我确实知道如何使用标志创建 1 个生产者 + 1 个消费者,并在一段时间内使用 wait() 。但是如何对多个消费者做同样的事情。

请看我的代码:

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");
}
}

最佳答案

这就是我要写的堆栈

public interface IntStack {
void push(int value);
int pop() throws InterruptedException;
}

public class DynamicStack implements IntStack {
private int size = 0, stack[];

public DynamicStack() {
this(16);
}

public DynamicStack(int capacity) {
stack = new int[capacity];
}

@Override
public synchronized void push(int item) {
if (size + 1 == stack.length)
stack = Arrays.copyOf(stack, stack.length * 2);
stack[size++] = item;
notifyAll(); // notify() would also do.
}

@Override
public synchronized int pop() throws InterruptedException {
while (size == 0)
wait();
return stack[--size];
}
}

关于java - 使用java线程的多监视器同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14651905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com