IllegalMonitorStateException

IllegalMonitorStateException

我正在尝试使用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;
    }

}

异常(exception)
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。

然后,当您最终想要将其解锁时,由于当前线程未持有该锁,则将引发IllegalMonitorStateException。

10-01 15:19