gpt4 book ai didi

java - 生产者/消费者线程不给出结果

转载 作者:行者123 更新时间:2023-12-01 15:24:16 34 4
gpt4 key购买 nike

我正在为我的操作系统类(class)做一个 CPU 调度模拟器项目。该程序应包含两个线程:生产者线程和消费者线程。生产者线程包括在系统中生成进程的生成器和选择多个进程并将它们放入一个名为 Buffer 的 ReadyQueue 类型的对象(消费者和生产者共享的对象)中的长期调度程序。消费者线程包括短期调度程序,它从队列中获取进程并启动调度算法。我在没有使用线程的情况下编写了整个程序,它工作正常,但现在我需要添加线程,但我从未使用过线程,所以如果有人可以向我展示如何修改下面显示的代码以实现所需的线程,我将不胜感激。

这是 Producer 类的实现:

public class Producer extends Thread{

ReadyQueue Buffer = new ReadyQueue(20); // Shared Buffer of size 20 between consumer and producer
JobScheduler js = new JobScheduler(Buffer);

private boolean systemTerminate = false; // Flag to tell Thread that there are no more processes in the system

public Producer(ReadyQueue buffer) throws FileNotFoundException{
Buffer = buffer;
Generator gen = new Generator(); // Generator generates processes and put them in a vector called memory
gen.writeOnFile();
}

@Override
public void run() {

synchronized(this){
js.select(); // Job Scheduler will select processes to be put in the Buffer

Buffer = (ReadyQueue) js.getSelectedProcesses();

while(!Buffer.isEmpty()){
try {
wait(); // When Buffer is empty wait until getting notification
} catch (InterruptedException e) {
e.printStackTrace();
}
systemTerminate = js.select();
Buffer = (ReadyQueue) js.getSelectedProcesses();
if(systemTerminate) // If the flag's value is true the thread yields
yield();
}
}
}

public ReadyQueue getReadyQueue(){
return Buffer;
}
}

这是 Consumer 类的实现:

public class Consumer extends Thread{

ReadyQueue Buffer = new ReadyQueue(20);
Vector<Process> FinishQueue = new Vector<Process>();
MLQF Scheduler ;
public Consumer(ReadyQueue buffer){
Buffer = buffer;
Scheduler = new MLQF(Buffer,FinishQueue); // An instance of the multi-level Queue Scheduler
}

@Override
public void run() {
int count = 0; // A counter to track the number of processes

while(true){
synchronized(this){
Scheduler.fillQueue(Buffer); // Take contents in Buffer and put them in a separate queue in the scheduler
Scheduler.start(); // Start Scheduling algorithm
count++;
}
if(count >= 200) // If counter exceeds the maximum number of processes thread must yeild
yield();
notify(); // Notify Producer thread when buffer is empty
}
}

public void setReadyQueue(ReadyQueue q){
Buffer = q;
}
}

这是主线程:

public class test {

public static void main(String[] args) throws FileNotFoundException,InterruptedException {
ReadyQueue BoundedBuffer = new ReadyQueue(20);
Producer p = new Producer(BoundedBuffer);
Consumer c = new Consumer(p.getReadyQueue());
p.start();
System.out.println("Ready Queue: "+p.getReadyQueue());
p.join();
c.start();
c.join();
}
}

提前谢谢您。

最佳答案

您的代码存在一个问题,即它存在多线程生产者/消费者模型中的常见错误。您必须使用while来查看wait()调用。例如:

try {
// we must do this test in a while loop because of consumer race conditions
while(!Buffer.isEmpty()) {
wait(); // When Buffer is empty wait until getting notification
...
}
} catch (InterruptedException e) {
e.printStackTrace();
}

问题是,如果您有多个正在使用的线程,您可能会通知一个线程,但随后另一个线程会通过并使刚刚添加的项目出列。当一个线程在收到通知后从 WAIT 队列移动到 RUN 队列时,它通常会被放在队列的末尾,可能位于等待在 this 上同步的其他线程后面。

有关更多详细信息,请参阅我的 documentation about this .

关于java - 生产者/消费者线程不给出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10403286/

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