gpt4 book ai didi

java - 生产者消费者最优解

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

我已经为生产者消费者问题编写了代码。下面是代码

package sample;

import java.util.List;
import java.util.ArrayList;

public class Interview2 {

public static void main(String[] args) {

List<Employee> empList = new ArrayList<Employee>();
Thread producer = new Thread(new Producer(empList , 4) , "Producer");
Thread consumer = new Thread(new Consumer(empList , 4) , "Consumer");

producer.start();
consumer.start();
}

}

class Employee
{
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
class Producer implements Runnable
{
List<Employee> empList;
int size;

public Producer(final List<Employee> empList , final int size)
{
this.empList = empList;
this.size = size;
}

public void run() {
for(int i=0; i<7;i++)
{
System.out.println("Produced "+i);
try {
produce(new Employee());
} catch (InterruptedException e) {
e.printStackTrace();
}
}


}

public void produce(Employee e) throws InterruptedException
{
while(empList.size()==size) // If list is full then will have to wait
{
synchronized(empList)
{
System.out.println("List is full "+Thread.currentThread().getName()+" Is waiting and" + " Size is "+empList.size());
empList.wait();
}
}

synchronized(empList)
{
empList.add(e);
empList.notify();
}
}


}


class Consumer implements Runnable
{
List<Employee> empList;
int size;

public Consumer(final List<Employee> empList , final int size)
{
this.empList = empList;
this.size = size;
}

public void run()
{
while(true)
{
try {
System.out.println("Consumed ");
Thread.sleep(50);
consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public void consume() throws InterruptedException
{
while(empList.isEmpty()) // If list is empty then will have to wait
{
synchronized(empList)
{
System.out.println("List is empty "+Thread.currentThread().getName()+" Is waiting and " + "Size is "+empList.size());
empList.wait();
}
}

synchronized(empList)
{
empList.remove(0);
empList.notifyAll();
}

}

}

但我希望这段代码就像生产者在消费者消费后在列表中添加一名员工,这意味着生产者和消费者之间必须进行切换。我想重复 10 个对象。请帮助我修改代码。提前致谢

最佳答案

由于这看起来像是某种作业,我将为您指明正确的方向,而不是提供代码本身:

您可以使用 ArrayBlockingQueue尺寸为 1 而不是 ArrayList :

ArrayBlockingQueue<Employee> empList = new ArrayBlockingQueue<Employee>(1);

ArrayBlockingQueue类提供两个阻塞调用即 puttake .通过使用这些方法,您可以摆脱当前通过 wait 进行的任何显式线程通信。和 notify .执行此操作的理想方法是调用 put Producer 中的方法线程和 take Consumer 中的方法线程 while环形。

由于队列的初始化大小为 1,任何试图在队列已满时向其插入新元素的线程都将在 put 时等待。在 while 循环中被调用。类似地,任何试图在队列为空时从队列中获取元素的线程都必须在 take 时等待。在 while 循环中被调用。

关于java - 生产者消费者最优解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30081880/

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