JDK-4076743 : it is not possible to see if an object is locked
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util.concurrent
  • Affected Version: 1.1.2,1.3.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic,solaris_2.5.1
  • CPU: generic,sparc
  • Submitted: 1997-09-04
  • Updated: 2006-12-08
  • Resolved: 2006-12-08
Related Reports
Duplicate :  
Description
Name: joT67522			Date: 09/04/97


The problem is that I do not want to access an object if it
is locked by another thread executing code inside a
synchronized block. But there is no way to know if an object
is locked without using some home grown method. My current
method for reducing the locks in the system is by cloning
my dataobjects when I want to (for example) write them to
a file. I am sure I am not the only one encountering similar
problems.

Since synchronized is a language feature I suppose it would
be preferable to add this to the language.

synchronizedNOWAIT(object) {
.
.
.
}

If the object is locked an exception (ObjectIsLockedException?)
would be thrown. I know this looks ugly, but you get the general
idea. Throwing an exception is not very pretty either but having
an alternate block to execute would be even uglier.

Maybe a queriable LockManager:

if(LockManager.lockIfNotLocked(object))
.
.
.
else
.
.
.

This would give the user the responsibility of unlocking
the object which could lead to undesireable effects and 
advanced debugging.

Another idea: a Lock object (this could be easily built if
we had a LockManager)

Lock lock = new Lock(object);

if(lock.locked())
...
else
...

The garbage collector would then take care of removing locks,
which is a bit better than the previous but still not good
enough.
company - MSC Konsult , email - ###@###.###
======================================================================

Comments
EVALUATION The java.lang.management.ThreadMXBean.getThreadInfo method provides you a programmatic way to find which lock a thread is blocked on and also which locks a thread currently owns. See http://java.sun.com/javase/6/docs/api/java/lang/management/ThreadInfo.html
09-12-2006

EVALUATION Library lock classes were added as part of 4486658: add concurrency library into Java core (JSR-166) ReentrantLock.tryLock() provides the requested functionality. For builtin locks, there is capability to query object locked-ness in the management API somewhere.
08-12-2006

EVALUATION I strongly echo the previous Evaluation that checking locks should be done with a library. It may well be that java.util.concurrent.Lock provides the desired functionality already.
07-12-2006

WORK AROUND Name: joT67522 Date: 09/04/97 ======================================================================
11-06-2004

EVALUATION The runtime is certainly able to support such a construct, so this is an RFE on the language. sheng.liang@Eng 1997-11-14 This should be evaluated in the context of proposed changes/extensions to Java's handling of concurrency in general. Ideally, this would be done via a magic library class rather than a language construct. gilad.bracha@eng 1998-01-23
23-01-1998