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


Yes, here when thread is in static synchronized method it must be holding lock on class’s class object and when it enters synchronized method it will hold lock on object’s monitor as well.
So, now thread holds 2 locks (it’s also called nested synchronization)-
>first one on class’s class object.
>second one on object’s monitor (This lock will be released when thread exits non static method). Now, let’s see a program to prove our point.



Program >
class MyRunnable1 implements Runnable{
   @Override
   public void run(){
          method1();
   }
   static synchronized void method1(){
          System.out.println("static synchronized method1() started");
          MyRunnable1 myRunnable1=new MyRunnable1();
          myRunnable1.method2();
          System.out.println("static synchronized method1() ended");
   }
   synchronized void method2(){
          System.out.println("in synchronized method2()");
   }
  
}
/** 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");
          thread1.start();       
         
         
   }
}
/*OUTPUT
static synchronized method1() started
in synchronized method2()
static synchronized method1() ended
*/
If you note output, when thread was in static synchronized method1() it was holding lock on class’s class object and it entered synchronized method2() by acquiring lock on object’s monitor as well.




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 :