Suppose you have 2 threads (Thread-1 and Thread-2) on same object. Thread-1 is in synchronized method1(), can Thread-2 enter static synchronized method2() at same time in java


Yes, here when Thread-1 is in synchronized method1() it must be holding lock on object’s monitor and Thread-2 can enter static synchronized method2() by acquiring lock on class’s class object.



Program >
class MyRunnable1 implements Runnable{
   @Override
   public void run(){
          if(Thread.currentThread().getName().equals("Thread-1"))
                 method1();
          else
                 method2();
   }
   static synchronized void method1(){
          System.out.println(Thread.currentThread().getName()+
                       " in static synchronized void method1() started");
          try {
                 Thread.sleep(2000);
          } catch (InterruptedException e) {
                 e.printStackTrace();
          }
          System.out.println(Thread.currentThread().getName()+
                       " in static synchronized void method1() ended");
   }
  
   synchronized void method2(){
          System.out.println(Thread.currentThread().getName()+
                       " in synchronized void method2() started");
          try {
                 Thread.sleep(2000);
          } catch (InterruptedException e) {
                 e.printStackTrace();
          }
          System.out.println(Thread.currentThread().getName()+
                       " in synchronized void method2() ended");
   }
  
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class MyClass {
   public static void main(String args[]) throws InterruptedException{
         
          MyRunnable1 myRunnable1=new MyRunnable1();
         
          Thread thread1=new Thread(myRunnable1,"Thread-1");
          Thread thread2=new Thread(myRunnable1,"Thread-2");
          thread1.start();       
          Thread.sleep(10);//Just to ensure Thread-1 starts before Thread-2
          thread2.start();       
         
         
   }
}
/*OUTPUT
Thread-1 in static synchronized void method1() started
Thread-2 in synchronized void method2() started
Thread-1 in static synchronized void method1() ended
Thread-2 in synchronized void method2() ended
*/
If you note output, when Thread-1 was in synchronized method1() it was holding lock on object’s monitor and Thread-2 entered static synchronized method2() by acquiring lock on class’s class object.




RELATED LINKS>

Situation based questions -

Suppose you have 2 threads (Thread-1 and Thread-2) on same object. Thread-1 is in synchronized method1(), can Thread-2 enter synchronized method2() at same time?

Suppose you have 2 threads (Thread-1 and Thread-2) on same object. Thread-1 is in static synchronized method1(), can Thread-2 enter static synchronized method2() at same time?

Suppose you have 2 threads (Thread-1 and Thread-2) on same object. Thread-1 is in synchronized method1(), can Thread-2 enter static synchronized method2() at same time?

Suppose you have thread and it is in synchronized method and now can thread enter other synchronized method from that method?

Suppose you have thread and it is in static synchronized method and now can thread enter other static synchronized method from that method?

Suppose you have thread and it is in static synchronized method and now can thread enter other non static synchronized method from that method?

Suppose you have thread and it is in synchronized method and now can thread enter other static synchronized method from that method?

Suppose you have 2 threads (Thread-1 on object1 and Thread-2 on object2). Thread-1 is in synchronized method1(), can Thread-2 enter synchronized method2() at same time?

Suppose you have 2 threads (Thread-1 on object1 and Thread-2 on object2). Thread-1 is in static synchronized method1(), can Thread-2 enter static synchronized method2() at same time?




Interviews >

THREADS - Top 80 interview questions and answers, important interview OUTPUT questions and answers, Set-2 > Q61- Q80





eEdit
Must read for you :