Skip to main content

Understanding Concurrency, Threading and Synchronization

Thread: A thread is defined at the operating system level. It is a single execution of a program.
A thread is a set of instruction. An application can be composed of several threads. Different threads can be executed at the same time. The JVM works with several threads like Garbage collector, JIT etc.

What does mean by "At the same time"?

  • In a single core of CPU, the threads cannot works at the same time, it give us a feel that it is running at same but in reality it work one by one and the context switch happen very fast in milliseconds which we can't observe.
  • On a multi-core CPU, things happens at the same time. A challenge while this is the read and write operation involve while running different thread at same time.
Who is responsible for the CPU sharing?
  • There is a special element who does this, is called scheduler.there are three reasons for a scheduled to pause a thread - 
  1. The CPU should be shared equally among threads.
  2. The thread might be waiting for some data .
  3. The thread might be waiting for another thread to do something.

Race Condition: Accessing data concurrently might lead to the issue. It means two different threads are trying to read and write the same variable at the same time called Race Condition.

How to prevent this?
To prevent this situation which causes issues can be prevented with synchronization. Synchronization prevents a block of code and ensure that it can only be executed by a single thread at a time.

To achieve synchronization, there is a concept called lock which is achieved using key. So, for synchronization to work we need a special technical object that will hold the key. In fact, every java object can play this role. It is also called monitor.Every object has a key.

How do you know which object is used for synchronization?

  • For static synchronized method, class itself is used as synchronization object and used to hold the key.
  • For non-static  synchronized method, instance is used as synchronization object, used to hold the key.
  • We can also use a dedicated object to synchronized.
Deadlock: A deadlock is a situation where a thread T1 holds a key needed by a thread T2 and T2 holds the key needed by T1. Thus both keeps waiting infinite for one another key which is called deadlock condition.

Runnable Pattern: The most basic way to create thread in java is using Runnable pattern. We can create a task using Runnable interface implementation.

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.
This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.

In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class

Thus to execute a task by thread happen in below steps -

  1. Create an instance of Runnable
  2. Create an instance of thread with task as a parameter
  3. Launch the thread by calling start() mentod on the instance of thread


A common mistake and important point to note here :

  • Do not call the run() method instead of the start() method.
  • Thread.currentThread() static method returns the current thread
How to stop thread?
It is more tricky. There is a method in the Thread class called stop() should not be called, it is also deprecated and not removed to support the code developed with older version of  java. It is just there for legacy and backward compatibility reasons. The right pattern is to use the interrupt() method.
The code of the task should call isInterurupted() to terminate itself. If the thread is blocked or waiting, it will throw InterruptedException.





Comments

Popular posts from this blog

Job Sequencing with Deadlines

Given a set of n jobs Each job i has an integer deadlines di>=0 and a profit pi>0 All jobs requires only one unit time to complete Only one machine is available for processing jobs For job i the profit pi is earned if the job is completed by its deadline.

Optimal Binary Search using Dynamic Programming

An optimal binary search tree is a binary search tree for which the nodes are arranged on levels such that the tree cost is minimum. If the probabilities of searching for elements of a set are known from accumulated data from past searches, then Binary Search Tree (BST) should be such that the average number of comparisons in a search should be minimum. eg. Lets the elements to be searched are A, B, C, D and probabilities of searching these items are 0.1, 0.2, 0.4 and 0.3 respectively. Lets consider 2 out of 14 possible BST containing these keys. Figure 1 Figure 2 Average number of comparison is calculated as sum of level*probability(key element) for each element of the tree. Lets the level of tree start from 1. Therefore, for figure 1 -     Average number of comparison = 1*0.1 +2*0.2 +3*0.4 +4*0.3  = 2.9                                  ...

Travel Sales Man Problem (TSP) and Solution using Dynamic Programming

Given a graph of n vertices, determine the minimum cost path to start at a given vertex and travel to each other vertex exactly once, returning to the starting vertex. In some versions, the starting and ending points are different and fixed, and all other points have to be visited exactly once from start to end. A standard way to solve these problems is to try all possible orders of visiting the n points, which results in a run-time of O(n!). To calculate cost using Dynamic Programming, we need to establish recursive relation in terms of sub-problems. Let us define a term C(S, i) be the cost of the minimum cost path visiting each vertex in set S exactly once, starting at 1 and ending at i. We start with all subsets of size 2 and calculate C(S, i) for all subsets where S is the subset, then we calculate C(S, i) for all subsets S of size 3 and so on. Note that 1 must be present in every subset. So the algorithm is like below - If size of S is 2, then S must be {1, i},  C(S, ...