If I want to ensure exclusive access to an object in Java, I can write something like this:
...
Zoo zoo = findZoo();
synchronized(zoo)
{
zoo.feedAllTheAnimals();
...
}
Is there a way to check if an object is currently locked? I don't want my thread to wait if another thread is accessing zoo
. If zoo
is not locked, I want my thread to acquire the lock and execute the synchronized
block; if not, I want it to skip it.
How can I do this?
Lock
be usingsynchronized
internally since it would be hard to avoid the low level API. If this is the case, how would itLock
be usingsynchronized
to achievetryLock
. – Adorl