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

StackOverFlowError and OutOfMemoryError in java

There are two area inside java virtual machine's memory the heap and the  stack . The  stack  memory is used to store local variables and function call while heap memory is used to store objects in  Java The most common cause of StackOverFlowError is too deep or infinite recursion or many local objects / variables creation inside function call in  Java.  According to the java source documentation,  Java throws  java.lang.StackOverflowError   when a stack overflow occurs because an application recurses too deeply. JVM has a given memory allocation for each stack of each thread, and if an attempt to call a method happens to fill this memory, JVM throws an error. Just like it would do if we try to write at index N of an array of length N.  The point to be noted here is that - These are errors not an exceptions. No memory corruption happens due to the error. Stack can not write into the heap space. A StackOverflowError i...

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.