Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java support for three different concurrency models

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.

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

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

like image 650
Ravindra babu Avatar asked Jul 25 '15 14:07

Ravindra babu


People also ask

What is concurrency model in Java?

A concurrency model specifies how threads in the the system collaborate to complete the tasks they are are given. Different concurrency models split the tasks in different ways, and the threads may communicate and collaborate in different ways.

How does Java support concurrency?

The backbone of Java concurrency is threads (a lightweight process, which has its own files and stacks and can access the shared data from other threads in the same process). The throughput and the interactivity of the program can be improved by performing time-consuming tasks asynchronously or in parallel.

Does Java support logical concurrency?

Java supports threads as part of the Java language via the Thread code. The Java application can create new threads via this class. Java 1.5 also provides improved support for concurrency with the java.

How does Java support concurrency and multithreading?

A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs.


1 Answers

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.
like image 178
V G Avatar answered Sep 23 '22 09:09

V G