Reputation: 2221
NOTE: Invalid question - see comment of @Bukhtoyarov Vladimir
Let's say we have the following code:
public class Main {
private Object monitor = new Object();
public static void main(String[] args) throws InterruptedException {
Main main = new Main();
main.test();
new Thread() {
@Override
public void run() {
try {
main.changeMonitor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
private void test() throws InterruptedException {
synchronized (monitor) {
Thread.sleep(100);
monitor = new Object();
Thread.sleep(1000);
System.out.println("test finished");
}
}
private void changeMonitor() throws InterruptedException {
Thread.sleep(600);
monitor = new Object();
System.out.println("monitor changed");
}
}
Here we have two threads - main thread and another worker thread. Also we have monitor
object. Inside worker thread we have next sequence of actions -
monitor
In main thread we are waiting 600ms and try to reassign monitor to a new object.
As the result - main thread is blocked - till worker thread releases lock on the monitor
object.
Here i have two questions
Concurrency in practice
book - the only way to be blocked by the lock aquision process - is to enter synchronization block. So why main thread is blocked till worker thread releases lock - in main thread we are not trying to enter synchronization blockmonitor
reference after 100ms, why main thread can not acquire lock on new reassigned object after 600ms ? I mean - after 600ms in monitor
ref is new object - so lock should be ready to be gained
The behavior is interesting - as i can not find any information about it in official Oracle docs or Concurrency in practice
book.Upvotes: 3
Views: 174
Reputation: 533640
This code
synchronized (monitor) {
is like
Object m = monitor;
synchronized (m) {
i.e. the read only happens once, and in a context which is not thread-safe.
why main thread can not lock on new object - reassigned inside worker thread.
This means
Upvotes: 5