Open In App

Importance of Thread Synchronization in Java

Last Updated : 11 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Thread synchronization in Java is important for managing shared resources in a multithreaded environment. It ensures that only one thread can access a shared resource at a time, which enhances the overall system performance and prevents race conditions and data corruption.

Why is Thread Synchronization Important?

In a multithreaded environment, threads may compete for shared resources i.e. files, memory, etc. Without synchronization, simultaneous access can lead

  • Race Conditions: Multiple Threads interchanging shared data at the same time and it results an unpredictable output.
  • Data Corruption: Incomplete or corrupted data when multiple threads modify the same resource simultaneously.

Real-world Example:

Imagine multiple computers connected to a single printer:

  • If two computers send print jobs simultaneously, the printer might mix their outputs and that leads to invalid results.
  • Similarly, threads accessing the same resource without coordination can produce inconsistent data.

Thread Priorities

In Java, thread priorities determine the execution order, allowing higher-priority threads to preempt lower ones and access resources first. However, when threads of equal priority compete for the same resource, conflicts can lead to inconsistent or erroneous outcomes.

Mechanisms for Thread Synchronization

Thread synchronization basically refers to The concept of one thread execute at a time and the rest of the threads are in waiting state. This process is known as thread synchronization. It prevents the thread interference and inconsistency problem.

Synchronization is build using locks or monitor. In Java, a monitor is an object that is used as a mutually exclusive lock. Only a single thread at a time has the right to own a monitor. When a thread gets a lock then all other threads will get suspended which are trying to acquire the locked monitor. So, other threads are said to be waiting for the monitor, until the first thread exits the monitor. In a simple way, when a thread request a resource then that resource gets locked so that no other thread can work or do any modification until the resource gets released.

Types of Thread Synchronization

Thread synchronization are of two types:

Mutual Exclusion

While sharing any resource, this will keep the thread interfering with one another i.e. mutual exclusive. We can achieve this via

  1. Synchronized Method
  2. Synchronized Block
  3. Static Synchronization

1. Synchronized Method

We can declare a method as synchronized using the synchronized keyword. This will make the code written inside the method thread-safe so that no other thread will execute while the resource is shared.

Implementation:

We will be proposing prints the two threads simultaneously showing the asynchronous behavior without thread synchronization. 

Example 1: Here, we will use non-synchronized method.

Java
// Extending Thread class
public class PrintTest extends Thread {
  
    // Non synchronized Code
    // Method 1
    public void printThread(int n)
    {
        // This loop will print the 
        // currently executed thread
        for (int i = 1; i <= 10; i++) {
            System.out.println("Thread " + n
                               + " is working...");

            // Try block to check for exceptions
            try {

                // Pause the execution of current thread
                // for 0.600 seconds using sleep() method
                Thread.sleep(600);
            }

            // Catch block to handle the exceptions
            catch (Exception ex) {

                // Overriding existing toString() method and
                // prints exception if occur
                System.out.println(ex.toString());
            }
        }

        // Display message for better readability
        System.out.println("--------------------------");

        try {

            // Pause the execution of current  thread
            // for 0.1000 millisecond or 1sec using sleep
            // method
            Thread.sleep(1000);
        }

        catch (Exception ex) {

            // Printing the exception
            System.out.println(ex.toString());
        }
    }
}

// Class 2
// Helper class extending Thread Class
public class Thread1 extends Thread {

    // Declaring variable of type Class1
    PrintTest test;

    // Constructor for class1
    Thread1(PrintTest p) { test = p; }

    // run() method of this class for
    // entry point for thread1
    public void run()
    {

        // Calling method  1 as in above class
        test.printThread(1);
    }
}

// Class 3
// Helper class extending Thread Class
public class Thread2 extends Thread {

    // Declaring variable of type Class1
    PrintTest test;

    // Constructor for class2
    Thread2(PrintTest p) { test = p; }

    // run() method of this class for
    // entry point for thread2
    public void run() { test.printThread(2); }
}

// Class 4
// Main class
public class SynchroTest {

    // Main driver method
    public static void main(String[] args)
    {

        // Creating object of class 1 inside main() method
        PrintTest p = new PrintTest();

        // Passing the same object of class PrintTest to
        // both threads
        Thread1 t1 = new Thread1(p);
        Thread2 t2 = new Thread2(p);

        // Start executing the threads
        // using start() method
        t1.start();
        t2.start();

        // This will print both the threads  simultaneously
    }
}

Output: 

Now using synchronized method, it will lock the object for the shared resource and gives the consistent output. 

Example 2: Below example, lock the object for the shared resource.

Java
// Java Program Illustrating Lock the Object for
// the shared resource giving consistent output
public class PrintTest extends Thread {
  
    // synchronized method will lock the object and
    // releases when thread is terminated 
    synchronized public void printThread(int n)
    {
        for (int i = 1; i <= 10; i++) {
            System.out.println("Thread " + n
                               + " is working...");

            try {

                // pause the execution of current  thread
                // for 600 millisecond
                Thread.sleep(600);
            }
            catch (Exception ex) {
              
                // overrides toString() method  and prints
                // exception if occur
                System.out.println(ex.toString());
            }
        }
        System.out.println("--------------------------");
        try {

            // pause the execution of current  thread for
            // 1000 millisecond
            Thread.sleep(1000);
        }
        catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }
}

// creating thread1 extending Thread Class
public class Thread1 extends Thread {

    PrintTest test;
    Thread1(PrintTest p) { test = p; }

    public void run() // entry point for thread1
    {

        test.printThread(1);
    }
}

// creating thread2 extending Thread Class
public class Thread2 extends Thread {

    PrintTest test;
    Thread2(PrintTest p) { test = p; }
    public void run()  // entry point for thread2
    {
        test.printThread(2);
    }
}

public class SynchroTest {
    public static void main(String[] args)
    {
        PrintTest p = new PrintTest();
      
        // passing the same object of class PrintTest to
        // both threads    
        Thread1 t1 = new Thread1(p);
        Thread2 t2 = new Thread2(p);
      
        // start function will execute the threads
        t1.start();
        t2.start();
    }
}

Output:

2. Synchronized Block

If we declare a block as synchronized, only the code which is written inside that block is executed sequentially not the complete code. This is used when we want sequential access to some part of code or to synchronize some part of code.

Syntax:

synchronized (object reference)
{
// Insert code here
}

Example: 

Java
// Java Program Illustrating  Synchronized Code
// Using synchronized block

// Helper class extending Thread class
class PrintTest extends Thread {

    // To print the thread
    public void printThread(int n)
    {

        // Making synchronized block that 
        // makes the block synchronized
        synchronized (this)
        {

            // Iterating using for loop
            for (int i = 1; i <= 10; i++) {

                System.out.println("Thread " + n
                                   + " is working...");

                // Try block to check for exceptions
                try {

                    // Making thread to pause for 0.6
                    // seconds
                    Thread.sleep(600);
                }

                // Catch block to handle exceptions
                catch (Exception ex) {

                    System.out.println(ex.toString());
                }
            }
        }

        System.out.println("--------------------------");

        try {

            // Making thread t osleep for 1 sec
            Thread.sleep(1000);
        }

        catch (Exception ex) {

            System.out.println(ex.toString());
        }
    }
}

// Helper class extending Thread class
class Thread1 extends Thread {

    PrintTest test;
    Thread1(PrintTest p) { test = p; }

    public void run() { test.printThread(1); }
}

// Helper class extending Thread class
class Thread2 extends Thread {

    PrintTest test;
    Thread2(PrintTest p) { test = p; }

    public void run() { test.printThread(2); }
}

// Main class
class SynchroTest {

    public static void main(String[] args) {

        // Creating instance for class 1 inside main()
        PrintTest p = new PrintTest();

        // Creating threads and
        // passing same object
        Thread1 t1 = new Thread1(p);
        Thread2 t2 = new Thread2(p);

        // Starting these thread using start() method
        t1.start();
        t2.start();
    }
}

Output: 

3. Static Synchronization

In this, the synchronized method is declared as "static" which means the lock or monitor is applied on the class not on the object so that only one thread will access the class at a time.

Example: 

Java
// Java Program Illustrate  Synchronized
// Using static synchronization

// Helper class
class PrintTest extends Thread {

    // Static synchronization locks the class PrintTest
    synchronized public static void printThread(int n)
    {

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

            System.out.println("Thread " + n
                               + " is working...");

            // Try block to check for exceptions
            try {

                // making thread to sleep for 0.6 seconds
                Thread.sleep(600);
            }

            // Catch block to handle the exceptions
            catch (Exception ex) {

                System.out.println(ex.toString());
            }
        }

        System.out.println("--------------------------");

        try {
            Thread.sleep(1000);
        }

        catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }
}

// Helper class extending Thread class
class Thread1 extends Thread {

    // run() method for thread
    public void run()
    {

        // Passing the class not the object
        PrintTest.printThread(1);
    }
}

// Helper class extending Thread class
class Thread2 extends Thread {

    public void run()
    {

        // Passing the class not the object
        PrintTest.printThread(2);
    }
}

// Main class
class SynchroTest {

    public static void main(String[] args)
    {

        // No shared object
        // Creating objects of class 2 and 3 that
        // are extending to Thread class
        Thread1 t1 = new Thread1();
        Thread2 t2 = new Thread2();

        // Starting thread with help of start() method
        t1.start();
        t2.start();
    }
}

Output: 


Next Article
Article Tags :
Practice Tags :

Similar Reads