Chris said:
I would assume so, in the absence of further information, but that assumption
should be validated by either reading the documentation carefully, or by
checking the source code (or both).
I would assume not, though I haven't checked the specs on this.
Consider this code fragment:
synchronized (myObject) {
myObject.doSomethingWonderful();
}
synchronized (myObject) {
myObject.doSomethingTerrible();
}
Suppose thread A enters the first synchronized block, and thread B
blocks waiting for myObject's monitor. Thread A releases the monitor at
the end of the first block, and B is then eligible to acquire it, but B
does not necessarily run right away. Thread A is likely to attempt to
reacquire myObject's monitor before B next runs, in which case I don't
think it safe to assume that the VM will have already assigned ownership
of the monitor to B. If B has not yet run and the monitor is still
unassigned when A attempts to enter the second synchronized block, then
I don't see any reason to be confident as to which thread will get the
monitor next.
No amount of code inserted between the two blocks in the example
fundamentally changes the argument, though anything that blocks A is
likely to persuade the VM to let B run for a while, being assured of
obtaining the monitor because of lack of contention for it.