|
|
|
|
|
by skyde
884 days ago
|
|
Why synchronized block are not preemptible?
When compiling public void syncMethod() { synchronized(lock) {
// some code
}
}they could translate to public void syncMethod() { await reentrantLockAsync.lockAsync();
try {
await somecodeAsync();
} finally {
await lock.unlockAsync()
}
}
|
|
The second issue is they're not completely equivalent. In the second case, you'd need extra memory for the `reentrantLock`, while `synchronized` works with any object. Furthermore, if you need to use `wait/notify`, then there need to be an extra `Condition` object to use in combination with the `ReentrantLock`. For sure, developers can rewrite most `synchronized` to use `ReentrantLock` and `Condition`, but javac won't do it automatically for you.