【发布时间】:2014-08-03 13:35:02
【问题描述】:
我哪里错了?即使我的消费者线程没有持有锁,程序也不会为任何锁调用(解锁/等待/信号)抛出 IllegalMonitorStateException。
更新:
private final ReentrantLock lock = new ReentrantLock();
private final Condition producers = lock.newCondition();
private final Condition consumers = lock.newCondition();
@Override
public void run() {
while (true) {
try {
//lock.lockInterruptibly();
try {
while (sharedResource.isEmpty()) {
printErr(name + " : Queue Empty ..");
consumers.await(500, TimeUnit.MILLISECONDS);
}
String obj = sharedResource.remove(0);
printOut(name + " : " + obj);
if (obj.equals(POISON_PILL)) {
sharedResource.add(POISON_PILL);
// System.err.println(name +" Taking break");
break;
}
producers.signal();
} finally {
lock.unlock();
}
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
// if(debug)System.err.println("Consumer Looping");
}
}
void java.util.concurrent.locks.ReentrantLock.unlock()
根据 Java Doc。
公共无效解锁()
尝试释放此锁。
如果当前线程是这个锁的持有者,那么持有计数就会递减。如果保持计数现在为零,则释放锁。 If the current thread is not the holder of this lock then IllegalMonitorStateException is thrown.
【问题讨论】:
-
这看起来像 Java 代码。为什么你也用c#标记它?
-
他们为什么会抛出异常?
标签: java multithreading locking