According to my understanding, the following piece of code should result in a deadlock. The reason being, when thread t1 locks static object firstData, he has acquired a lock on the class. So, when he tries to lock another static object secondData, the request should block.
However, the program runs fine and prints *** Successfully acquired both the locks
What is it about locking static objects that im missing 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 the locks are on object, instead of class, please take a look at this