๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Static Synchronization  


Introduction
  • A static synchronization in Java is used to perform synchronization on class-level resources by locking the class object rather than a specific instance.
  • Synchronizing at the class level ensures that only one thread at a time can execute any synchronized static method of that class.
  • Points to Remember:
    • Class-Level Locking: Static synchronization locks the Class object, not the instance, so it applies to all objects of that class.
    • Shared Data Protection: Useful when working with static variables or shared resources that need to be thread-safe.
    • Global Effect: If one thread is executing a synchronized static method, other threads are blocked from executing any other synchronized static method of the same class.
    • Performance: Since it locks at the class level, it may reduce concurrency if used excessively.
  • Syntax:-
    class MyClass
    {
        static synchronized void myMethod()
        {
            // code
        }
    }
  • Program:
    class BookMovieSeat
    {
        static int total_seats = 10;
    
        static synchronized void bookSeat(int seats, String name)
        {
            if (total_seats >= seats)
            {
                System.out.println(name + " booked " + seats + " seats successfully");
                total_seats = total_seats - seats;
                System.out.println("Total seats left : " + total_seats);
            }
            else
            {
                System.err.println(name + " sorry...!! seats not left");
                System.err.println("Total seats left : " + total_seats);
            }
        }
    }
    
    class MyThread extends Thread
    {
        BookMovieSeat bts;
        int seats;
    
        public MyThread(BookMovieSeat bts, int seats)
        {
            this.bts = bts;
            this.seats = seats;
        }
    
        public void run()
        {
            bts.bookSeat(seats, Thread.currentThread().getName());
        }
    }
    
    public class MovieBookingApp
    {
        public static void main(String[] args)
        {
            BookMovieSeat bms1 = new BookMovieSeat();
    
            MyThread deepak = new MyThread(bms1, 6);
            deepak.setName("Deepak");
            deepak.start();
    
            BookMovieSeat bms2 = new BookMovieSeat();
    
            MyThread amit = new MyThread(bms2, 5);
            amit.setName("Amit");
            amit.start();
        }
    }
  • Explanation:
    • Here we used a static synchronized method bookSeat(), which locks the class object BookMovieSeat.class instead of individual instances.
    • The total_seats variable is static, so it is shared among all instances of the class.
    • Even though two objects are created (bms1 and bms2), only one thread at a time can execute the static synchronized method for the class, ensuring thread safety.
    • Because of static synchronization:
      • When Deepak thread starts executing bookSeat(), Amit thread must wait until Deepak releases the class-level lock.
      • After Deepak finishes execution and releases the lock, Amit will enter the method and check the updated total_seats.
    • This ensures correct seat count, prevents race conditions, and avoids data inconsistency, even when multiple objects are used.
    • Static synchronization is useful when you have shared class-level resources that must be accessed by multiple threads safely.