[concurrency-interest] Observability of Semaphore

Doug Lea dl@cs.oswego.edu
Wed, 18 Jun 2003 06:55:00 -0400


> Basically, I am implementing a conditional tree where nodes in the
> tree have true/false values, and I need to support nodes whose value
> is derived from whether a lock style object has been acquired or not

The JSR-166 ReentrantLock class, which we are strongly encouraging
people use for most "fancy" locking, contains an isLocked() method
that tells you if any thread holds the lock.  It provides only a
momentary snapshot. In general you do not know if the lock is about to
be released. So you have to be very careful when using it.  In most
cases, you are better off insted using a tryLock -- getting the lock
only if not already held, which tells you the same thing and enters
the lock if free so you can safely access at any fields the lock is
protecting and prevents others from getting lock until you release.

-Doug