Reputation: 31212
Is there a way to get something like isLocked
status of a Lock?
What I am doing is acquiring a lock before a loop operation and releasing it only after a certain condition is met AND the lock is already locked. So I need it ask the lock if it is already locked and I can't seem to find a way in the API to do it.
Upvotes: 4
Views: 2796
Reputation: 3158
What you're doing is not going to work. A call to an isLocked
method will only tell you that a lock was locked at some instant of time during the call. It does this by returning true. If it returns false, it has told you nothing at all. (As the ReentrantLock documentation hints, this can be useful in that if it returns true, you know some other thread somewhere does lock it; code to do so exists and sometimes executes. If it returns false, you know nothing. Call this method enough times, and you might get useful statistical information--which seems to be what this method is for.)
There's no limit to the possibilities. If isLocked
returns true, 500 threads could have locked it, done tons of work each, and released it before the statement after the call starts to execute. If it returns false, the same thing could have happened. And it either case you have no clue as to whether it's locked when your next statement starts to execute. (And if you knew it was locked at the start, you would know nothing about the situation 2 nanoseconds after the start.)
You need to rethink your synchronization a bit. Perhaps accept the need for multiple locking threads. Or, if you really want to pass locking responsibility from thread to thread, maybe some kind of relay-race-baton field, so each thread knows precisely what it's responsible for.
Upvotes: 0
Reputation: 5094
It sounds like you want to have the lock only for the first few iterations of the loop, but keep looping without the lock afterward. This is awkward at best. You'd be far safer with something like.
lock.lock();
try
{
while (!certainCondition() && loopCondition())
{
doWork();
}
} finally
{
lock.unlock();
}
while (loopCondition())
{
continueWork();
}
This still seems odd, as I can't think of a situation where you'd need to only protect the first few loops then break out.
Upvotes: 0
Reputation: 14699
You can use tryLock()
to get the Lock
if it's free.
Lock
is an interface, so as long as all you need is any implementation of a Lock
, then ReentrantLock
has an isLocked()
method; however, I am not certain why you would only want it if it is currently locked.
Make sure that this is not a case of the XY-Problem. Or are you just saying that you wouldn't need to unlock it if it were already unlocked?
Upvotes: 4
Reputation: 3484
What implementation are you using?
ReentrantLock
class has it
So you may want to use this type as reference, not interface
Upvotes: 1
Reputation: 2095
if (lock.tryLock())
{
// Got the lock
try
{
// Process record
}
finally
{
// Make sure to unlock so that we don't cause a deadlock
lock.unlock();
}
}
else
{
// Someone else had the lock, abort
}
see this
Upvotes: 1