Locking Mechanism on shared resource in java

Posted By : Pavan Kumar | 28-Sep-2020

Java

Loading...

Multithreading is the most powerful and interesting topic in Java. Here, we will be discussing how we can lock the shared resources among various threads.

PREREQUISITES

1. Any text editor (NotePad,NotePad++,eclipse ,etc...)

2. Jdk installed on the computer.

Lets cover the multithreading and locking in java.

Whenever we write a program sometimes there could be a situation where some common resource will be shared by multiple threads. When multiple threads access shared resources then it could be a situation where data consistency is not possible because each thread runs asynchronously. So in java we have a mechanism to restrict the common resource to be shared only by a single thread at a time and this process is known as Synchronization . synchronized is a keyword in java which is generally used for locking.

Synchronization provides two types of locking Implementation -

1. Object level locking.

2. Class level locking.

Object level locking -

In this locking mechanism, first of all a thread has to get lock for that object. when the thread got the lock only then it is allowed to access the common resource and the other threads are not allowed to access the same resource. Once the execution on shared resource is completed, the existing thread will releases the lock and the resource is acquired by another waiting thread. Acquiring and releasing of locks is not handled by the developer, JVM is responsible for it.

Lets understand this through an example.

There is a Resource class which has a sharedResource method in which we are trying to display 10 statements each by two threads named as t1 and t2. But when the program starts running , the output shows before one thread stops execution, the other thread enters in the sharedResource method.

Demo 1 -

class Resource implements Runnable

{

void sharedResource(String threadName){

for(int i = 1; i <= 10 ; i++){

System.out.println(threadName + " "+i);

}

}

public void run(){

sharedResource(Thread.currentThread().getName());

}

}

class ThreadDemo{

public static void main(String []args){

Data d1 = new Data();

Thread t1 = new Thread(d1);

Thread t2 = new Thread(d1);

t1.setName("THREAD ONE");

t2.setName("THREAD TWO");

t1.start();

t2.start();

}

}

Output -

THREAD TWO 1

THREAD TWO 2

THREAD TWO 3

THREAD TWO 4

THREAD ONE 1

THREAD TWO 5

THREAD ONE 2

THREAD TWO 6

THREAD ONE 3

THREAD TWO 7

THREAD ONE 4

THREAD TWO 8

THREAD TWO 9

THREAD TWO 10

THREAD ONE 5

THREAD ONE 6

THREAD ONE 7

THREAD ONE 8

THREAD ONE 9

THREAD ONE 10

***************

THREAD TWO 1

THREAD ONE 1

THREAD TWO 2

THREAD ONE 2

THREAD TWO 3

THREAD ONE 3

THREAD TWO 4

THREAD ONE 4

THREAD ONE 5

THREAD ONE 6

THREAD ONE 7

THREAD ONE 8

THREAD ONE 9

THREAD ONE 10

THREAD TWO 5

THREAD TWO 6

THREAD TWO 7

THREAD TWO 8

THREAD TWO 9

THREAD TWO 10

***************

THREAD ONE 1

THREAD ONE 2

THREAD ONE 3

THREAD ONE 4

THREAD ONE 5

THREAD ONE 6

THREAD ONE 7

THREAD ONE 8

THREAD ONE 9

THREAD ONE 10

THREAD TWO 1

THREAD TWO 2

THREAD TWO 3

THREAD TWO 4

THREAD TWO 5

THREAD TWO 6

THREAD TWO 7

THREAD TWO 8

THREAD TWO 9

THREAD TWO 10

Now we are going to apply object level locking using synchronized blocks on critical sections where we don’t want interference of another thread in between while one thread is in process.

Demo 2 -

class Resource implements Runnable

{

void sharedResource(String threadName){

synchronized(this){//object level lock

for(int i = 1; i <= 10 ; i++){

System.out.println(threadName + " "+i);

}

}

}

public void run(){

sharedData(Thread.currentThread().getName());

}

}

class ThreadDemo{

public static void main(String []args){

Data d1 = new Data();

Thread t1 = new Thread(d1);

Thread t2 = new Thread(d1);

t1.setName("THREAD ONE");

t2.setName("THREAD TWO");

t1.start();

t2.start();

}

}

OUTPUT:

THREAD ONE 1

THREAD ONE 2

THREAD ONE 3

THREAD ONE 4

THREAD ONE 5

THREAD ONE 6

THREAD ONE 7

THREAD ONE 8

THREAD ONE 9

THREAD ONE 10

THREAD TWO 1

THREAD TWO 2

THREAD TWO 3

THREAD TWO 4

THREAD TWO 5

THREAD TWO 6

THREAD TWO 7

THREAD TWO 8

THREAD TWO 9

THREAD TWO 10

***************

THREAD TWO 1

THREAD TWO 2

THREAD TWO 3

THREAD TWO 4

THREAD TWO 5

THREAD TWO 6

THREAD TWO 7

THREAD TWO 8

THREAD TWO 9

THREAD TWO 10

THREAD ONE 1

THREAD ONE 2

THREAD ONE 3

THREAD ONE 4

THREAD ONE 5

THREAD ONE 6

THREAD ONE 7

THREAD ONE 8

THREAD ONE 9

THREAD ONE 10

Let's check out one more example of object level locking implementation in java. In the below example there are three threads t1, t2 and t3. Thread t1 and t2 are working on object r1 and thread t3 is working on object r2. Now when the program runs and whichever thread runs first , there is no interference of t1 and t2. When either t1 runs first or t2 runs first , t3 can come in between because this thread is accessing shared data through object d2 but out of t1 and t2 , any one of them first completes execution then only another thread will run after that.

Demo 3 -

class Resource implements Runnable

{

void sharedResource(String threadName){

synchronized(this){//object level lock

for(int i = 1; i <= 10 ; i++){

System.out.println(threadName + " "+i);

}

}

}

public void run(){

sharedResource(Thread.currentThread().getName());

}

}

class ThreadDemo{

public static void main(String []args){

Resource r1 = new Resource();

Resource r2 = new Resource();

Thread t1 = new Thread(r1);

Thread t2 = new Thread(r1);

Thread t3 = new Thread(r2);

t1.setName("THREAD ONE");

t2.setName("THREAD TWO");

t3.setName("THREAD THREE");

t1.start();

t2.start();

t3.start();

}

}

OUTPUT:

THREAD TWO 1

THREAD TWO 2

THREAD TWO 3

THREAD TWO 4

THREAD TWO 5

THREAD TWO 6

THREAD TWO 7

THREAD TWO 8

THREAD TWO 9

THREAD TWO 10

THREAD ONE 1

THREAD ONE 2

THREAD ONE 3

THREAD THREE 1

THREAD ONE 4

THREAD THREE 2

THREAD ONE 5

THREAD THREE 3

THREAD ONE 6

THREAD ONE 7

THREAD ONE 8

THREAD ONE 9

THREAD ONE 10

THREAD THREE 4

THREAD THREE 5

THREAD THREE 6

THREAD THREE 7

THREAD THREE 8

THREAD THREE 9

THREAD THREE 10

***************

THREAD ONE 1

THREAD ONE 2

THREAD ONE 3

THREAD ONE 4

THREAD ONE 5

THREAD ONE 6

THREAD ONE 7

THREAD ONE 8

THREAD ONE 9

THREAD ONE 10

THREAD TWO 1

THREAD TWO 2

THREAD TWO 3

THREAD TWO 4

THREAD TWO 5

THREAD TWO 6

THREAD TWO 7

THREAD TWO 8

THREAD TWO 9

THREAD TWO 10

THREAD THREE 1

THREAD THREE 2

THREAD THREE 3

THREAD THREE 4

THREAD THREE 5

THREAD THREE 6

THREAD THREE 7

THREAD THREE 8

THREAD THREE 9

THREAD THREE 10

***************

THREAD TWO 1

THREAD TWO 2

THREAD TWO 3

THREAD TWO 4

THREAD TWO 5

THREAD THREE 1

THREAD THREE 2

THREAD TWO 6

THREAD THREE 3

THREAD TWO 7

THREAD THREE 4

THREAD TWO 8

THREAD THREE 5

THREAD THREE 6

THREAD THREE 7

THREAD THREE 8

THREAD THREE 9

THREAD THREE 10

THREAD TWO 9

THREAD TWO 10

THREAD ONE 1

THREAD ONE 2

THREAD ONE 3

THREAD ONE 4

THREAD ONE 5

THREAD ONE 6

THREAD ONE 7

THREAD ONE 8

THREAD ONE 9

THREAD ONE 10

Class level locking -

Class level locking is applicable on the entire class and not on any object. Let's compare class level locking with object level locking by taking an example from Demo 3.

Demo 4 -

class Resource implements Runnable

{

void sharedResource(String threadName){

synchronized(Resource.class){//class level lock

for(int i = 1; i <= 10 ; i++){

System.out.println(threadName + " "+i);

}

}

}

public void run(){

sharedData(Thread.currentThread().getName());

}

}

class ThreadDemo{

public static void main(String []args){

Resource r1 = new Resource();

Resource r2 = new Resource();

Thread t1 = new Thread(r1);

Thread t2 = new Thread(r1);

Thread t3 = new Thread(r2);

t1.setName("THREAD ONE");

t2.setName("THREAD TWO");

t3.setName("THREAD THREE");

t1.start();

t2.start();

t3.start();

}

}

OUTPUT:

THREAD ONE 1

THREAD ONE 2

THREAD ONE 3

THREAD ONE 4

THREAD ONE 5

THREAD ONE 6

THREAD ONE 7

THREAD ONE 8

THREAD ONE 9

THREAD ONE 10

THREAD THREE 1

THREAD THREE 2

THREAD THREE 3

THREAD THREE 4

THREAD THREE 5

THREAD THREE 6

THREAD THREE 7

THREAD THREE 8

THREAD THREE 9

THREAD THREE 10

THREAD TWO 1

THREAD TWO 2

THREAD TWO 3

THREAD TWO 4

THREAD TWO 5

THREAD TWO 6

THREAD TWO 7

THREAD TWO 8

THREAD TWO 9

THREAD TWO 10

***************

THREAD TWO 1

THREAD TWO 2

THREAD TWO 3

THREAD TWO 4

THREAD TWO 5

THREAD TWO 6

THREAD TWO 7

THREAD TWO 8

THREAD TWO 9

THREAD TWO 10

THREAD ONE 1

THREAD ONE 2

THREAD ONE 3

THREAD ONE 4

THREAD ONE 5

THREAD ONE 6

THREAD ONE 7

THREAD ONE 8

THREAD ONE 9

THREAD ONE 10

THREAD THREE 1

THREAD THREE 2

THREAD THREE 3

THREAD THREE 4

THREAD THREE 5

THREAD THREE 6

THREAD THREE 7

THREAD THREE 8

THREAD THREE 9

THREAD THREE 10

We are an ERP development company that specializes in building custom enterprise solutions to overcome complex business challenges and streamline operations. Our ERP development services enable organizations to efficiently manage their business processes, sales data, and track on-site activities with ease. We have successfully completed several full-scale ERP projects for cross-industry enterprises. For more information, send us your queries at [email protected]