Monday, October 2, 2017

Concurrency Learning from Sir Jenkov's Site

If a thread reads a memory location while another thread writes to it, what value will the first thread end up reading? The old value? The value written by the second thread? Or a value that is a mix between the two? Or, if two threads are writing to the same memory location simultanously, what value will be left when they are done? The value written by the first thread? The value written by the second thread? Or a mix of the two values written?

Multithreading and Java:

 Java had multithreading capabilities from the very beginning.


  1. 1. New, asynchronous "shared-nothing" platforms and APIs like Vert.x and Play / Akka and Qbit have emerged. These platforms use a different concurrency model than the standard Java / JEE concurrency model of threading, shared memory and locking.
  2. New non-blocking concurrency algorithms have been published, and new non-blocking tools like the LMax Disrupter have been added to our toolkits.
  3. New functional programming parallelism has been introduced with the Fork and Join framework in Java 7, and the collection streams API in Java 8.


Benifits of Multithreading:

i) Better resource utilization.
ii) Simpler program design in some situations.
iii) More responsive programs.

Multithreading Cost:

i) More complex design:
Code executed by multiple threads accessing shared data need special attention. Thread interaction is far from always simple. Errors arising from incorrect thread synchronization can be very hard to detect, reproduce and fix.

ii) Context Switching Overhead:
When a CPU switches from executing one thread to executing another, the CPU needs to save the local data, program pointer etc. of the current thread, and load the local data, program pointer etc. of the next thread to execute. This switch is called a "context switch". The CPU switches from executing in the context of one thread to executing in the context of another.

Context switching isn't cheap. You don't want to switch between threads more than necessary.
iii) Increased Resource Consumption:
Try creating a program that creates 100 threads that does nothing but wait, and see how much memory the application takes when running.


Concurrency Model:

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

Concurrency Models and Distributed System: Similarities and Dissim:

1. In a concurrent system different threads communicate with each other. In a distributed system different processes communicate with each other (possibly on different computers). Threads and processes are quite similar to each other in nature. That is why the different concurrency models often look similar to different distributed system architectures.

2. Of course distributed systems have the extra challenge that the network may fail, or a remote computer or process is down etc. But a concurrent system running on a big server may experience similar problems if a CPU fails, a network card fails, a disk fails etc. The probability of failure may be lower, but it can theoretically still happen.

Parallel Workers Disadvantages:

1. Shared State Can Get Complex:
i) The shared workers often need access to some kind of shared data, either in memory or in a shared database. Threads need to avoid race conditions, deadlock and many other shared state concurrency problems.

ii) Additionally, part of the parallelization is lost when threads are waiting for each other when accessing the shared data structures.


Resource Link: http://tutorials.jenkov.com/java-concurrency/concurrency-models.html


No comments:

Post a Comment