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

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

Important points on Classes and Methods in Java as per Java Language Specification

Class Declarations: A class declaration specifies a new named reference type. There are two kinds of class declarations: normal class declarations and enum declarations. It is a compile-time error if a class has the same simple name as any of its enclosing classes or interfaces. Class Modifiers: A class declaration may include class modifiers. The access modifier public pertains only to top level classes and member classes, not to local classes or anonymous classes. The access modifiers protected and private pertain only to member classes within a directly enclosing class declaration. The modifier static pertains only to member classes, not to top level or local or anonymous classes. It is a compile-time error if the same keyword appears more than once as a modifier for a class declaration. abstract Classes: An abstract class is a class that is incomplete, or to be considered incomplete. It is a compile-time error if an attempt is made to create an instance o...

Web Service Explorer (An Amazing tool in eclipse IDE)

Web Service Explorer (An Amazing tool in eclipse IDE) Developer or user who wants to use the web-service has to consume the web-service and invoke the method of the service he/she wants to. Thus there are many potential ways to test the we-service exposed. • write a CICS program that invokes the Web Service • consume the WSDL and make a client project in eclipse IDE or My-Eclipse and invoke the service • generate a Java program using Rational Application Developer (RAD) or WebSphere Developer for  zSeries (WDz), • use the debugger that comes with WDz, • use the supplied Web Services Explorer that comes with RAD and WDz, • use the TCP/IP Monitor that comes with RAD and WDz, or a combination of these. Apart from these, one method to test the web-service on the ease using the web service explorer tool available in eclipse IDE. For the tutorial of how to use visit the below official link of tutorial. http://www.eclipse.org/webtools/jst/components/ws/M4/tu...