问题描述
我在多线程环境中经历了不同的并发模型()
I am going through different concurrency model in multi-threading environment (http://tutorials.jenkov.com/java-concurrency/concurrency-models.html)
本文重点介绍三个并发模型。
-
并行工人
第一个并发模型就是我所说的并行工模型。传入的工作分配给不同的工作人员。
The first concurrency model is what I call the parallel worker model. Incoming jobs are assigned to different workers.
装配线
工人在工厂的装配线上被组织成工人。每个工人只执行完整工作的一部分。当该部分完成时,工作人员将作业转发给下一个工作人员。
The workers are organized like workers at an assembly line in a factory. Each worker only performs a part of the full job. When that part is finished the worker forwards the job to the next worker.
每个工作人员都在自己的线程中运行,并且不与其他工作人员共享任何状态。这有时也被称为无共享并发模型。
Each worker is running in its own thread, and shares no state with other workers. This is also sometimes referred to as a shared nothing concurrency model.
功能并行
函数并行的基本思想是使用函数调用实现程序。函数可以被视为代理或 actors ,它们相互发送消息,就像在流水线并发模型(AKA反应或事件驱动系统)中一样。当一个函数调用另一个函数时,这类似于发送消息。
The basic idea of functional parallelism is that you implement your program using function calls. Functions can be seen as "agents" or "actors" that send messages to each other, just like in the assembly line concurrency model (AKA reactive or event driven systems). When one function calls another, that is similar to sending a message.
现在我想映射java API支持这三个概念
Now I want to map java API support for these three concepts
-
并行工作者:它是,, API?
Parallel Workers : Is it ExecutorService,ThreadPoolExecutor, CountDownLatch API?
装配线:向 JMS &使用队列& ;;的消息传递概念主题即可。
Assembly Line : Sending an event to messaging system like JMS & using messaging concepts of Queues & Topics.
功能并行:在某种程度上& java 8流。与溪流相比,ForkJoin池易于理解。
Functional Parallelism: ForkJoinPool to some extent & java 8 streams. ForkJoin pool is easy to understand compared to streams.
我是否正确映射这些并发模型?如果不是,请纠正我。
Am I correct in mapping these concurrency models? If not please correct me.
推荐答案
这些模型中的每一个都说明了从一般的角度来看工作是如何完成/分割的,但是当涉及到实施时,它实际上取决于您的确切问题。通常我会这样看:
Each of those models says how the work is done/splitted from a general point of view, but when it comes to implementation, it really depends on your exact problem. Generally I see it like this:
- 并行工人:生产者在某处创建新工作(例如在
BlockingQueue
)和许多线程(通过ExecutorService
)并行处理这些作业。当然,您也可以使用CountDownLatch
,但这意味着您希望在完全N
子问题之后触发操作已处理完毕(例如,您知道您的大问题可能会被分解为N
较小的问题,请检查)。 - 装配线 :对于每个中间步骤,你有一个
BlockingQueue
和一个Thread
或一个ExecutorService
。在每一步中,作业都来自一个BlickingQueue
并放入下一个,以便进一步处理。根据您的想法使用JMS:JMS用于连接分布式组件,并且是Java EE的一部分,并且不被认为在高并发上下文中使用(消息通常保留在硬盘上,然后再进行处理)。 - 功能并行:
ForkJoinPool
就如何实现这一点就是一个很好的例子。
- Parallel Workers: a producer creates new jobs somewhere (e.g in a
BlockingQueue
) and many threads (via anExecutorService
) process those jobs in parallel. Of course, you could also use aCountDownLatch
, but that means you want to trigger an action after exactlyN
subproblems have been processed (e.g you know your big problem may be split inN
smaller problems, check the second example here). - Assembly Line: for every intermediate step, you have a
BlockingQueue
and oneThread
or anExecutorService
. On each step the jobs are taken from oneBlickingQueue
and put in the next one, to be processed further. To your idea with JMS: JMS is there to connect distributed components and is part of the Java EE and was not thought to be used in a high concurrent context (messages are kept usually on the hard disk, before being processed). - Functional Parallelism:
ForkJoinPool
is a good example on how you could implement this.
这篇关于Java支持三种不同的并发模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!