In my opinion, the following code fragment should lead to deadlock. The reason is that when thread t1 blocks the static firstData object, it acquired a lock on the class. Therefore, when it tries to block another static secondData object, the request should block.
However, the program runs fine and prints *** Successfully acquired both the locks
What is this blocking of static objects that are not here?
public class Deadlock { public static void main(String[] args) { Thread t1 = new Thread(new DeadlockRunnable()); t1.start(); } } class DeadlockRunnable implements Runnable { static Object firstData = new Object(); static Object secondData = new Object(); public void run() { synchronized(firstData) { synchronized(secondData) { System.out.println("*** Successfully acquired both the locks"); } } } }
For all those who answered that locks are on the object, instead of a class, see this
source share