Synchronization concepts in java threads

The main use of a thread to execute in parallel with the application’s main thread or with other threads is the main feature of the Java concurrent programming. But this useful property causes a lot of trouble for programmers which must assure a correct sharing of the resources used in common by different threads. It means that the threads that execute in parallel and use common critical resources like database or files must be synchronized at some points to assure a correct functioning of the application.

The notion of correct functioning in concurrent and parallel programming means avoiding conflicts of simultaneously performing by several threads same operation which could not be performed in parallel, like writing in a buffer stream, updating same table data etc. The fragments of program code which perform such operations are called critical sections. In this context the synchronization means that a thread must wait for another thread to leave the critical section, if it has already entered this section, and to enter into this section using some security measures.

To achieve the synchronization in java we have keyword: synchronized

We can use the synchronized keyword only for methods and blocks.

To understand how synchronized keyword work, we will take one example; see below example method of a class which can be access by more than one thread at a time.

package com.java.connect.thread;
 
public class CommonResource {
 
    public synchronized void test1() {
    // do some stuff which should be access by one thread at a time
    }
 
    public void test2() {
    // do some other stuff which can be access by any thread at any time
    }
}

Object lock:

When any thread creating the object of the class CommonResource. And try to access the synchronized method, the thread require the object lock, one newly created object called one object lock.

CommonResource cr  = new CommonResource();
cr.test1();// require the object lock
cr.test2();// do not require object lock

Whenever any object reference of a thread is trying to access a synchronized method or block with other object, than it can access only by object lock.

For every object there is object lock.

In our example if two thread are using the same object reference of CommonResourse class, than they can be access the test1() method at same time.

If thread 1 has the lock it can enter into test1 () method of CommonResourse class. To give access to second thread it has to release the lock by coming out from the method.

The test2() method of CommonResourse is not synchronized, so there is no need to acquire the object lock to access it.

So by using one object reference synchronized block or method can be accessed by one thread at a time.

Class lock:

Here you can think about creating the new object to give the access in synchronized block at a time to more thread, yes it is possible because for each object there is object lock.

To prevent to come across this problem better to defined the method as static and synchronized, because the static method or blocks are class level.

class CommonResource {
 
    public static synchronized void test1() {
        // do some stuff.
    }
 
    public void test2() {
        // do some other stuff
    }
}

There is class lock for each class level similar to object lock for per object. So to access the class synchronized method you must require the class lock.

public static void main(String[] args) {
    CommonResource cr  = new CommonResource();
    CommonResource.test1();// require the class lock
    cr.test2();// do not require object lock
}

Synchronization is very useful concept in java thread programming; this topic is mostly asked in java thread interview.