gpt4 book ai didi

使用 ReentrantLock 时出现 java.lang.IllegalMonitorStateException 错误?

转载 作者:行者123 更新时间:2023-11-29 04:38:24 27 4
gpt4 key购买 nike

我正在尝试使用 ReentrantLock 实现阻塞 FIFO。除了抛出 IllegalMonitorStateException 外,一切正常。当我们尝试释放未被线程锁定的资源时,可能会发生此错误。但是我找不到为什么会出现此错误。

package edu.utdallas.blockingFIFO;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import edu.utdallas.taskExecutor.Task;

public class ArrayBlockingQueue implements BlockingFIFOque{
private final Task[] arr;
private int arrSize;
private int start;
private int end;
private int ocupied;
private final Lock mlock = new ReentrantLock();
private Condition Empty = mlock.newCondition();
private Condition Full = mlock.newCondition();

public ArrayBlockingQueue(int Size) {

arrSize = Size;
start = 0;
end = 0;
ocupied = 0;
arr = new Task[arrSize];
}

@Override
public void put(Task item) throws Exception {

mlock.tryLock();
try{
while(ocupied == arrSize) Full.await();
ocupied++;
arr[end++]=item;
if(end > arrSize-1){
end = end - arrSize;
}
Empty.signalAll();
} finally{
mlock.unlock();
}
}


@Override
public Task take() throws Exception {

Task item;
mlock.tryLock();
try{
while(ocupied == 0) Empty.await();
ocupied = ocupied - 1;
item = arr[start];
start++;
if(start > arrSize-1){
start = start - arrSize;
}
Full.signal();
}finally{
mlock.unlock(); //Error here only
}

return item;
}

}

异常

java.lang.IllegalMonitorStateException******  Adding Task SimpleTask141
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(Unknown Source)
at java.util.concurrent.locks.ReentrantLock.unlock(Unknown Source)
at edu.utdallas.blockingFIFO.ArrayBlockingQueue.take(ArrayBlockingQueue.java:61)
at edu.utdallas.taskExecutorImpl.TaskExecutorImpl$1.run(TaskExecutorImpl.java:38)

最佳答案

不要使用

tryLock()

但是

lock()

因为tryLock()会返回一个boolean值,这意味着如果可能它会尝试加锁,但是如果锁已经被另一个线程加锁,它会立即返回false。

然后当你想在finally中解锁时,由于锁没有被当前线程持有,会抛出IllegalMonitorStateException。

关于使用 ReentrantLock 时出现 java.lang.IllegalMonitorStateException 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40275653/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com