One thread entered synchronized method on same instance, no other threads can enter any other synchronized methods on the same instance. - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Sunday, August 21, 2016

One thread entered synchronized method on same instance, no other threads can enter any other synchronized methods on the same instance.

As mention in the previous post, All objects have their own implicit monitor associated with them.

When multiple thread of same instance created in the application, One thread entered monitor through synchronized method on instance, this thread acquired locked on object's monitor and will release the lock on object's monitor only when it exits from synchronized method. if one thread has lock on object's monitor through method1(), other threads can not execute other synchronized methods of that class object.

Now understand above concept with example.

We have created MyRunnable class implemented Runnable interface, having two synchronized methods display1() and display2(). Two Threads Thread-0 and Thread-1 is created on target MyRunnable object. When Thread-0 is in synchronized display1() method, it is holding lock on object's monitor and will release lock until it exits from synchronized display1() method. So Thread-1 have to waitfor Thread-0 to release lock on object's monitor so that it could enter synchronized display2() method.

Code

public class MyRunnable implements Runnable {

 @Override
 public void run() {
  if (Thread.currentThread().getName().equals("Thread-0"))
   display1();
  else
   display2();
 }

 public synchronized void display1() {
  System.out.println(Thread.currentThread().getName()
    + " is acquired object lock in display1() method.");
  for (int i = 1; i <= 2; i++) {
   System.out.println(Thread.currentThread().getName()
     + " is executing. Value of i=" + i);
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  System.out.println(Thread.currentThread().getName()
    + " is completed in display1() method.");
 }

 public synchronized void display2() {
  System.out.println(Thread.currentThread().getName()
    + " is acquired object lock in display2() method.");
  for (int i = 1; i <= 2; i++) {
   System.out.println(Thread.currentThread().getName()
     + " is executing. Value of i=" + i);
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  System.out.println(Thread.currentThread().getName()
    + " is completed in display2() method.");
 }
}
public class SyncDemo {

 public static void main(String[] args) throws InterruptedException {
  // create an runnable target object
  MyRunnable myRunnable = new MyRunnable();
  Thread t1 = new Thread(myRunnable);
  Thread t2 = new Thread(myRunnable);
  t1.start();
  Thread.sleep(5); // make sure that thread t1 will start first
  t2.start();
 }
}

Output of the above program

Thread-0 is acquired object lock in display1() method.
Thread-0 is executing. Value of i=1
Thread-0 is executing. Value of i=2
Thread-0 is completed in display1() method.
Thread-1 is acquired object lock in display2() method.
Thread-1 is executing. Value of i=1
Thread-1 is executing. Value of i=2
Thread-1 is completed in display2() method.

If you notice output, when Thread-0 was is in synchronized display1() it was holding lock on object’s monitor. So, Thread-1 was waiting for Thread-0 to release lock on object’s monitor to enter synchronized display2().

1 comment :

  1. factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
    factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
    factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.


    ReplyDelete