According to my understanding, every object has a lock that can be hold by the current thread.
So, based on this example:
public class T1 {
private final Object o = new Object();
public void m1() {
synchronized(o) {
//...wait some time
}
}
}
If I create something like:
T1 subject1 = new T1();
T1 subject2 = new T1();
Runnable r1 = new Runnable() {
@Override
public void run() {
subject1.m1();
subject2.m1();
}
};
Thread t1 = new Thread(r1);
t1.start();
There will be two locks, subject1.o and subject2.o (I tested they are different instances), which means both methods will run independently and at the same time, well, in my example one is executed and then, after it releases the lock, the next runs although subject1 and subject2 are different instances with different locks.
Why?