Java synchronized question Java synchronized question multithreading multithreading

Java synchronized question


  1. Yes, using the synchronized method modifier on a non-static method means that it uses the monitor of the instance the method is invoked on, and this is shared between all such methods.
  2. No - the thread already owns the monitor, so it is free to enter other blocks protected by the same monitor.


  1. See here:

    it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.

  2. Since this thread holds the lock on the current object, it can invoke method2(), and no other thread can.


A note on question 2, method1() can also call synchronized methods also in other classes which could cause a deadlock:

Thread1 calls synchronized method1() which in turn needs to call synchronized method_b() in AnotherClassThread2 holds the lock on AnotherClass and is executing a method that needs to call method1() in the class whose lock is held by Thread1

Both Threads will block waiting for the other to free the lock, a deadlock.