Thread Synchronization in Java

In this module, we are going to discuss thread synchronization in Java. First, let us look at what is thread synchronization, and what use does it provide? thread synchronization will help us to maintain threads in multithreading without conflicting one thread with another thread.

The conflicts often arise are solved between threads are like when one thread is reading data the other thread is prevented from modifying the reading data this is called synchronization. To implement this synchronization Java Programming has a concept called “monitor”. This monitor is not a defined class it is a process mechanism that explains how synchronization can be performed.

When one thread enters into a monitor all the other threads must wait until the entered thread comes out of the monitor.

Implementation of Thread Synchronization in Java Programming

In Java Programming the synchronization can be performed using synchronized statements. The following is the syntax of the synchronized statement.

synchronized(obj reference){
/* statmet1; statment2; ........ */     }
  • obj reference: It refers to the object being synchronized.
  • statements: These are statements to be synchronized.

The following code gives a complete understanding of synchronized threads with multithreaded programming.

Without using the synchronized method: Thread synchronization in Java

import java.io.*;
import java.util.*;
class base
{
    void sum(int a,int b)
    {                                           //this is class shared by two thread objects
        System.out.println(a+b);
        try{
            Thread.sleep(500);               
        }catch(Exception e)
        {
            System.out.println("Thread is disturbed");
        }
        System.out.println("After thread execution");
    }
}
class useclass extends Thread{
    Thread t=new Thread(this);
    int a;
    int b;
    base bas;
    useclass(base bas,int a,int b)
    {
       this.bas=bas;
        this.a=a;
        this.b=b;
        t.start();
    }
    public void run()               //runnable method for running thread
    {
        bas.sum(a,b);
    }
}
public class mulitthread
{
    public static void main(String[] args)
    {
        base bz=new base();
        useclass us1=new useclass(bz,32,22);          //we created two objects for runnable threads meaning two threads are created
        useclass us2=new useclass(bz,35,36);
        try{
            us1.join();                  //wait for thread to be terminated
            us2.join();
        }catch(Exception e)
        {
            System.out.println("Thread is distrubted");
        }
    }
}

Output:

54
71
After thread execution
After thread execution

Explanation:

Here, we can see clearly that the “After thread execution” statement must be printed after 54 and after 72 but here the statements are printed after both the execution of the two objects. The threads are not synchronized.

Using synchronized method in Java:

import java.io.*;
import java.util.*;
class base
{
    void sum(int a,int b)
    {
        System.out.println(a+b);
        try{
            Thread.sleep(500);
        }catch(Exception e)
        {
            System.out.println("Thread is disturbed");
        }
        System.out.println("After thread execution");
    }
}
class useclass extends Thread{
    Thread t=new Thread(this);
    int a;
    int b;
    base bas;
    useclass(base bas,int a,int b)
    {
       this.bas=bas;
        this.a=a;
        this.b=b;
        t.start();
    }
    public void run()
    {
        synchronized(bas)              //Synchronized block
        {
        bas.sum(a,b);                  //Synchronized statments
        }
    }
}
public class mulitthread
{
    public static void main(String[] args)
    {
        base bz=new base();
        useclass us1=new useclass(bz,32,22);
        useclass us2=new useclass(bz,35,36);  
        try{
            us1.join();
            us2.join();
        }catch(Exception e)
        {
            System.out.println("Thread is distrubted");
        }
    }
}

Output:

54
After thread execution
71
After thread execution

Explanation:

Here, we can see after adding the synchronized block the thread is fully terminated before beginning another thread execution.

Also read: How to stop a running thread in Java

Leave a Reply