简体   繁体   English

Java多线程通信

[英]Java multi thread communication

I'm relatively new using Java multi-threading and concurrence tools . 我是使用Java多线程和并发工具的新手。 I'm implementing an app which has an event producer (let's call it EventProducer ). 我正在实现一个具有事件生成器的应用程序(我们将其称为EventProducer )。 EventProducer has a thread pool FixedThreadPool , where one new thread per event to process is sent, submiting a new EventProcessor thread to the pool. EventProducer有一个线程池FixedThreadPool ,每个事件要发送一个新线程,将一个新的EventProcessor线程提交给该池。

That's going all right, a thread in the pool is being created per event arrived. 没关系,每个事件到达时都会在池中创建一个线程。 But problem is that I want to communicate that EventProcessor thread (which is specific for each event request) with two threads that are supposed to be application scoped, I mean, I'll have only an instance of them in the application, let's say Service1 and Service2 . 但是问题是我想将EventProcessor线程(特定于每个事件请求)与应该被应用程序范围限制的两个线程进行通信,我的意思是,我在应用程序中将只有它们的一个实例,比如说Service1Service2 Both of them have thread pools, to be able to process the tasks concurrently. 它们都具有线程池,以便能够同时处理任务。

That specific EventProcessor sends sets of tasks to Service1 and it returns responses for each task. 该特定的EventProcessor将一组任务发送到Service1 ,并返回每个任务的响应。 For that responses, I have looked for CompletionService but I don't know how can I integrate it with Blocking queues for bi-directional communication. 对于这些响应,我一直在寻找CompletionService,但我不知道如何将其与Blocking队列集成以进行双向通信。 After, depending on that response, EventProcessor sends one action or another to execute in Service2 . 之后,根据该响应, EventProcessor发送一个或另一个动作以在Service2执行。 Service2 will also send a response about that action to EventProcessor . Service2还将有关该操作的响应发送到EventProcessor

Anybody knows about how can I tackle such problem? 有人知道我该如何解决这个问题? I need an introduction in order to achieve the first steps. 我需要一个介绍才能实现第一步。 Pool your ideas. 汇集您的想法。

The best object for inter-thread communications is the BlockingQueue . 线程间通信的最佳对象是BlockingQueue They are super-flexible and thread safe and will usually handle all requirements. 它们具有超柔韧性和线程安全性,通常可以满足所有要求。

BlockingQueue<Task> queue = new LinkedBlockingQueue<>();

Let you Service1 and 2 be ExecutorService s. 让您将Service1和2作为ExecutorService Your EventProcessor sends tasks in the form of Callables to Service1, using a return type R that encapsulates your response. 您的EventProcessor使用封装您的响应的返回类型R以Callables的形式将任务发送到Service1。 It also queues up the resulting Futures. 它还将产生的期货排队。 In another thread, you receive those responses and dispatch new callables to Service2. 在另一个线程中,您将收到这些响应并将新的可调用对象分派到Service2。 You can also use a CompletionService for that. 您也可以为此使用CompletionService。

What if you have a non-blocking queue in your event dispatcher thread, such as the ConcurrentLinkedQueue . 如果事件分配器线程中有一个非阻塞队列,例如ConcurrentLinkedQueue ,该怎么办? You then pass a reference to that into your Callable so it can add responses to the queue. 然后,您将对它的引用传递到Callable以便它可以将响应添加到队列中。
In your event dispatcher you could then periodically poll the queue to see if it has anything interesting in it. 然后,您可以在事件调度程序中定期轮询队列,以查看队列中是否有有趣的东西。 As the queue is non blocking it would just return null when it's empty. 由于队列是非阻塞的,因此在队列为null时将仅返回null

Finally I managed to solve it in the following way: EventProducer creates services Service1 and Service2 , which are runnable objects, and launches them as threads. 最终,我设法通过以下方式解决了该问题: EventProducer创建服务Service1Service2 ,它们是可运行的对象,并将它们作为线程启动。 Each of these service keeps waitting for result on its pool, which is implemented as CompletionService . 这些服务中的每一个都在其池中等待结果,该池实现为CompletionService Those service reference is keeped in EventProducer and passed by reference to every EventProcessor , which is created when an event is launched. 这些服务引用保留在EventProducer并通过引用传递给每个EventProcessor ,该事件在启动事件时创建。

EventProcessor has two BlockingQueue elements, each of them is used to receive responses from the services. EventProcessor具有两个BlockingQueue元素,每个元素用于接收来自服务的响应。 When I want to call a service, I do it trough the object's reference and call the dispatch method of the service, passing a collection of elements I want to process as parameter and also the reference of EventProcessor itself. 当我想调用服务时,我通过对象的引用并调用服务的调度方法,将要处理的元素的集合传递为参数,并传递EventProcessor本身的引用。 The call is done following the thread of EventProcessor , but it only creates a callable object per object to process, executing code into the service class, which submits the new threads to his pool. 该调用是在EventProcessor线程之后完成的,但是它仅针对每个要处理的对象创建一个可调用对象,将代码执行到服务类中,该服务类将新线程提交到其池中。

When the result is given, the service, which is waiting for it in his own thread, puts it into the BlockingQueue of the EventProcessor , because it has his reference. 给出结果后,正在其自己的线程中等待其结果的服务会将其放入EventProcessorBlockingQueue中,因为它具有其引用。 After EventProcessor manages the results into his own thread (and can send other actions to other services). 之后, EventProcessor将结果管理到自己的线程中(并可以将其他操作发送到其他服务)。

That's all, many thanks for people helping, you've your upvotes because every answer has clarified me some concept. 这就是全部,非常感谢您提供的帮助,因为您的每一个回答都向我阐明了一些概念,您对此表示感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM