实现生产者消费者模式 [英] Implementing Producer consumer pattern

查看:142
本文介绍了实现生产者消费者模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图实现一个典型的



我刚写了一个骨架,骨架无法正常工作。



MailProducer.java

  public class MailProducer implements Callable< Void> 
{

@Override
public Void call()throws Exception
{
System.out.println(inside mail Producer);
System.out.println(Thread executing =+
Thread.currentThread()。getName());
return null;
}

}

MailConsumer.java

  public class MailConsumer implements Callable< Void> 
{

@Override
public Void call()throws Exception
{
System.out.println(inside mail consumer);
System.out.println(Thread executing =+
Thread.currentThread()。getName());
return null;
}

}

,最后是Executor



MailExecutor.java

  public class MailExecutor 
{

private static final int NTHREADS = 25;
private static final ExecutorService exec =
Executors.newFixedThreadPool(NTHREADS);

public static void main(String [] args)
{
exec.submit(new MailConsumer());
exec.submit(new MailProducer());

System.out.println(inside main);

}

}

运行程序,我期望它来回生产者和消费者保持打印在相应的类中写的。但是,相反,程序挂起/打印以下行后什么也不做。出了什么问题?

输出 ...(输出不是我预期的,出了什么问题?)

 内部邮件消费者
内部main
线程执行=池-1线程-1
内部邮件
Thread executing = pool-1-thread-2


解决方案

您缺少共享队列。没有队列,你什么都没有。



生产者把工作放到队列上。消费者将工作从队列中删除。使用 BlockingQueue ,其 put() take() 方法呼叫。在不同的线程中运行生产者和消费者,可以在调用这些方法时安全地阻止。



生产者和消费者都不需要 Callable ; Runnable 会做。使用 执行人 将它们捆绑在一起是一个好主意。


I am trying to write a mail utility that places mails in a queue, and it is later consumed by a consumer thread.

I am trying to implement a typical producer-consumer pattern, but something is going wrong.

I just wrote a skeleton , and the skeleton is not working as expected.

MailProducer.java

public class MailProducer implements Callable<Void>
 {

@Override
public Void call() throws Exception
{
    System.out.println("inside mail Producer");
    System.out.println("Thread executing = " +
                           Thread.currentThread().getName());
    return null;
}

}

MailConsumer.java

public class MailConsumer implements Callable<Void>
{

@Override
public Void call() throws Exception
{
    System.out.println("inside mail consumer");
    System.out.println("Thread executing = " + 
                        Thread.currentThread().getName());
    return null;
}

 }

and finally the Executor

MailExecutor.java

  public class MailExecutor
  {

private static final int NTHREADS = 25;
private static final ExecutorService exec = 
                Executors.newFixedThreadPool(NTHREADS);

public static void main(String[] args)
{
    exec.submit(new MailConsumer());
    exec.submit(new MailProducer());

    System.out.println("inside main");

}

  }

Now when I run the program, I expect it to go back and forth the producer and consumer to keep printing what is written in the respective classes. But instead , the program hangs/does nothing after printing the below lines . What is going wrong ? Am I missing something?

Output ...(Output is not what I had expected. What is going wrong ?)

   inside mail consumer
   inside main
   Thread executing = pool-1-thread-1
   inside mail Producer
   Thread executing = pool-1-thread-2

解决方案

You are missing the shared queue. Without the queue, you have nothing.

Producers put work onto the queue. Consumers take work off the queue. Use a BlockingQueue, whose put() and take() methods are blocking calls. Running producers and consumers in separate threads allows them to safely block while calling these methods.

Neither the producers nor the consumers need to be Callable; Runnable will do. Using an Executor to tie it all together is a good idea.

这篇关于实现生产者消费者模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆