gpt4 book ai didi

java - 使用队列的生产者/消费者线程

转载 作者:IT老高 更新时间:2023-10-28 20:24:17 26 4
gpt4 key购买 nike

我想创建某种 Producer/Consumer 线程应用程序。但我不确定在两者之间实现队列的最佳方式是什么。

所以我提出了两个想法(这两个想法都可能完全错误)。我想知道哪个会更好,如果它们都很糟糕,那么实现队列的最佳方法是什么。我关心的主要是我在这些示例中对队列的实现。我正在扩展一个 Queue 类,它是一个内部类并且是线程安全的。下面是两个示例,每个示例有 4 个类。

主类-

public class SomeApp
{
private Consumer consumer;
private Producer producer;

public static void main (String args[])
{
consumer = new Consumer();
producer = new Producer();
}
}

消费类-

public class Consumer implements Runnable
{
public Consumer()
{
Thread consumer = new Thread(this);
consumer.start();
}

public void run()
{
while(true)
{
//get an object off the queue
Object object = QueueHandler.dequeue();
//do some stuff with the object
}
}
}

生产者类-

public class Producer implements Runnable
{
public Producer()
{
Thread producer = new Thread(this);
producer.start();
}

public void run()
{
while(true)
{
//add to the queue some sort of unique object
QueueHandler.enqueue(new Object());
}
}
}

队列类-

public class QueueHandler
{
//This Queue class is a thread safe (written in house) class
public static Queue<Object> readQ = new Queue<Object>(100);

public static void enqueue(Object object)
{
//do some stuff
readQ.add(object);
}

public static Object dequeue()
{
//do some stuff
return readQ.get();
}
}

主类-

public class SomeApp
{
Queue<Object> readQ;
private Consumer consumer;
private Producer producer;

public static void main (String args[])
{
readQ = new Queue<Object>(100);
consumer = new Consumer(readQ);
producer = new Producer(readQ);
}
}

消费类-

public class Consumer implements Runnable
{
Queue<Object> queue;

public Consumer(Queue<Object> readQ)
{
queue = readQ;
Thread consumer = new Thread(this);
consumer.start();
}

public void run()
{
while(true)
{
//get an object off the queue
Object object = queue.dequeue();
//do some stuff with the object
}
}
}

生产者类-

public class Producer implements Runnable
{
Queue<Object> queue;

public Producer(Queue<Object> readQ)
{
queue = readQ;
Thread producer = new Thread(this);
producer.start();
}

public void run()
{

while(true)
{
//add to the queue some sort of unique object
queue.enqueue(new Object());
}
}
}

队列类-

//the extended Queue class is a thread safe (written in house) class
public class QueueHandler extends Queue<Object>
{
public QueueHandler(int size)
{
super(size); //All I'm thinking about now is McDonalds.
}

public void enqueue(Object object)
{
//do some stuff
readQ.add();
}

public Object dequeue()
{
//do some stuff
return readQ.get();
}
}

走吧!

最佳答案

Java 5+ 拥有完成此类工作所需的所有工具。你会想要:

  1. 将您的所有生产者放在一个 ExecutorService ;
  2. 把你所有的消费者放到另一个ExecutorService
  3. 如有必要,请使用 BlockingQueue 在两者之间进行通信.

我对 (3) 说“如有必要”,因为根据我的经验,这是一个不必要的步骤。您所做的只是将新任务提交给消费者执行器服务。所以:

final ExecutorService producers = Executors.newFixedThreadPool(100);
final ExecutorService consumers = Executors.newFixedThreadPool(100);
while (/* has more work */) {
producers.submit(...);
}
producers.shutdown();
producers.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
consumers.shutdown();
consumers.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);

所以producers直接提交给consumers

关于java - 使用队列的生产者/消费者线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2332537/

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