Java support for three different concurrency models
Asked Answered
J

3

28

I am going through different concurrency model in multi-threading environment (http://tutorials.jenkov.com/java-concurrency/concurrency-models.html)

The article highlights about three concurrency models:

  1. Parallel Workers

The first concurrency model is what I call the parallel worker model. Incoming jobs are assigned to different workers.

  1. Assembly Line

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.

  1. Functional Parallelism

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.

Now I want to map java API support for these three concepts

  1. Parallel Workers : Is it ExecutorService,ThreadPoolExecutor, CountDownLatch API?

  2. Assembly Line : Sending an event to messaging system like JMS & using messaging concepts of Queues & Topics.

  3. 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.

Johnathan answered 25/7, 2015 at 14:24 Comment(0)
C
18

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:

  1. Parallel Workers: a producer creates new jobs somewhere (e.g in a BlockingQueue) and many threads (via an ExecutorService) process those jobs in parallel. Of course, you could also use a CountDownLatch, but that means you want to trigger an action after exactly N subproblems have been processed (e.g you know your big problem may be split in N smaller problems, check the second example here).
  2. Assembly Line: for every intermediate step, you have a BlockingQueue and one Thread or an ExecutorService. On each step the jobs are taken from one BlickingQueue 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).
  3. Functional Parallelism: ForkJoinPool is a good example on how you could implement this.
Conscionable answered 4/8, 2015 at 10:33 Comment(2)
For functional parallelism, please check akka.io , best actor model based framework built for jvm.Nancienancy
At the end of his tutorial, the author says "Java EE just doesn't have the edge anymore, I feel". Is it really the case? Haven't these techniques been incorporated in Java EE yet? or Don't they have any plans to add in future?Interlaminate
B
6

An excellent question to which the answer might not be quite as satisfying. The concurrency models listed show some of the ways you might want to go about implementing an concurrent system. The API provides tools used to implementing any of these models.

Lets start with ExecutorService. It allows you to submit tasks to be executed in a non-blocking way. The ThreadPoolExecutor implementation then limits the maximum number of threads available. The ExecutorService does not require the task to perform the complete process as you might expect of a parallel worker. The task may be limited to specific part of the process and send a message upon completion that starts the next step in an assembly line.

The CountDownLatch and the ExecutorService provide a means to block until all workers have completed that may come in handy if a certain process has been divided to different concurrent sub-tasks.

The point of JMS is to provide a means for messaging between components. It does not enforce a specific model for concurrency. Queues and topics denote how a message is sent from a publisher to a subscriber. When you use queues the message is sent to exactly one subscriber. Topics on the other hand broadcast the message to all subscribers of the topic.

Similar behavior could be achieved within a single component by for example using the observer pattern.

ForkJoinPool is actually one implementation of ExecutorService (which might highlight the difficulty of matching a model and an implementation detail). It just happens to be optimized for working with large amount of small tasks.

Summary: There are multiple ways to implement a certain concurrency model in the Java environment. The interfaces, classes and frameworks used in implementing a program may vary regardless of the concurrency model chosen.

Bluejacket answered 4/8, 2015 at 5:12 Comment(1)
awesome! Just a note, each of those models suits some different use cases that can be realized using the highlighted Java util classes judiciously.Odyssey
S
0

Actor model is another example for an Assembly line. Ex: akka

Shapely answered 19/9, 2022 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.