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

时间:2016-10-27 03:30:36

标签: java multithreading java.util.concurrent

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

1 个答案:

答案 0 :(得分:3)

不要使用

tryLock()

但是

lock()

因为tryLock()将返回一个布尔值,这意味着它会尽可能地锁定,但如果锁被另一个线程锁定,它将立即返回false。

然后当你想在最后解锁时,由于当前线程没有持有锁,将抛出IllegalMonitorStateException。