>>On a different note, for the specific case where there are only 2
>>threads, is it guaranteed that if one thread has the lock on an object
[quoted text clipped - 5 lines]
> 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.

Signature
John Bollinger
jobollin@indiana.edu
Chris Smith - 03 Dec 2005 04:45 GMT
> I would assume not, though I haven't checked the specs on this.
In fact, John is correct. The specification makes no guarantees
whatsoever about the order of acquiring monitors. If a fair lock is
needed, one can be implemented on top of the basic monitor primitives.
For example, Java 1.5's ReentrantLock class takes a boolean parameter to
the constructor asking if it should be fair or not. The proposition
from earlier in this thread still doesn't hold, though, due to the
tryLock method. Another implementation of the 1.5 Lock class would need
to be devised to ensure that this proposition holds true.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Chris Uppal - 03 Dec 2005 12:02 GMT
> [...] Java 1.5's ReentrantLock class takes a boolean parameter to
> the constructor asking if it should be fair or not. The proposition
> from earlier in this thread still doesn't hold, though, due to the
> tryLock method. Another implementation of the 1.5 Lock class would need
> to be devised to ensure that this proposition holds true.
Or one could just refrain from using the tryLock() method.
-- chris
Chris Uppal - 03 Dec 2005 11:03 GMT
> > > On a different note, for the specific case where there are only 2
> > > threads, is it guaranteed that if one thread has the lock on an object
[quoted text clipped - 7 lines]
>
> I would assume not, though I haven't checked the specs on this.
We seem to be thinking at cross-purposes. I was replying on the assumption
(unconsidered and quite possibly wrong) that Andy was asking about the locks in
the new concurrency stuff. If we are talking about the locks at the Java/JVM
level then indeed all bets are off.
(I note that Chris Smith believes that at least the new ReentrantLocks don't,
in fact, have the property in question. I haven't checked, but feel fairly
confident that he's right.)
-- chris