Reputation: 181
I'm developing for Android 2.2, and a bit confused as to how ReentrantLocks work. Can the following code ever throw an IllegalMonitorStateException? I ask because I don't see how it can--according to the API, tryLock returns true if and only if the lock is successfully obtained--but occasionally the unlock() command does.
public void lockDemo() {
ReentrantLock myLock = new ReentrantLock();
if (myLock.tryLock()) {
System.out.println("Lock obtained");
myLock.unlock();
}
}
Upvotes: 2
Views: 2678
Reputation: 66866
From the javadoc: unlock()
throws IllegalMonitorStateException
if the current thread does not hold the lock. I'm guessing you would not be asking unless you are seeing this, but, you should check the stack trace to see what method is triggering it. The example you give won't reproduce this situation, so your real code must be more complex and the problem must lie somewhere in the other bits.
Off the top of my head, I can imagine that maybe the lock is actually being unlocked twice somehow. Or that the lock may not actually be successfully acquired in some code paths that think they have the lock.
Upvotes: 1
Reputation: 55
The tryLock()
mothod takes two parameters, long timeout, TimeUnit unit
. That could have something to do with it.
I'm not saying that way of using locks is wrong, I've just never used them that way. I've been taught to use locks in this fashion:
public void lockDemo() {
ReentrantLock myLock = new ReentrantLock();
try {
myLock.lock();
// do work...
} catch (Exception e) {
// catch if something fails
} finally {
myLock.unlock();
}
}
Upvotes: 0
Reputation: 4031
It might not actually solve your problem, but there's some discussion of something similar here: http://www.jroller.com/ethdsy/entry/reentrantlock_bug
Upvotes: 0