为什么在illegalMonitorState例外此代码的结果?(Why does this cod

2019-07-03 17:57发布

下面的代码试图插入一个随机值成圆形队列并将其删除。 然而,也有一些同步问题。 我知道我可以使用更高级别的程序,我打算这样做,对于生产代码,但我很好奇,为什么这不起作用? 我缺少的是在这里吗?

public class CircularQueue {
int count;
int rear;
int front;
Object lock = new Object();
int size;
int[] array;
CircularQueue(int size)
{
    this.size= size;
    array = new int[size];
}

void enqueue(int number) throws InterruptedException
{
    if(isFull())
        lock.wait();

    synchronized(lock)
    {

        array[rear] = number;
        System.out.println("Rear is:"+ rear+ "value is:"+number+"Size is:"+size);

        rear = (rear+1)%size;
        count++;
    }
    lock.notify();

}

void dequeue() throws InterruptedException
{
    if(isEmpty())
        lock.wait();

    synchronized(lock)
    {
        int retVal = 0;
        retVal = array[front];
        System.out.println("Front is:"+ front+ "value is:"+retVal);

        front = (front+1)%size;
        count--;
    }

    lock.notify();

}

boolean isFull()
{
    if(count == size)
    {
        return true;
    }
    else
        return false;

}

boolean isEmpty()
{
    return count == 0;
}
}

// Test类

import java.util.Random;
public class App {

    public static void main(String[] args) throws InterruptedException
    {
       final Random random = new Random();
       final CircularQueue circularQueue = new CircularQueue(10);
       Thread t1 = new Thread(new Runnable(){

        @Override
        public void run() {
            try {
                circularQueue.enqueue(random.nextInt(100));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

       });
       Thread t2 = new Thread(new Runnable(){

            @Override
            public void run() {
                try {
                    circularQueue.dequeue();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

           });

       t1.start();
       t2.start();
       t1.join();
       t2.join();

    }

}

Answer 1:

因为java.lang.Object#waitjava.lang.Object#notifyjava.lang.Object#notifyAll ,必须从synchronized块调用。

作为解决方案(需要检查),你应该把你的条件synchronized块内:

void enqueue(int number) throws InterruptedException
{

    synchronized(lock)
    {
        if(isFull())
           lock.wait();

        array[rear] = number;
        System.out.println("Rear is:"+ rear+ "value is:"+number+"Size is:"+size);

        rear = (rear+1)%size;
        count++;
        lock.notify();
    }
}

void dequeue() throws InterruptedException
{
    synchronized(lock)
    {
        if(isEmpty())
           lock.wait();

        int retVal = 0;
        retVal = array[front];
        System.out.println("Front is:"+ front+ "value is:"+retVal);

        front = (front+1)%size;
        count--;
        lock.notify();
    }

}


Answer 2:

在此代码中的另一个问题是,即使的isEmpty / isFull返回true - 直到调用相邻等待队列的状态可能会改变。
例如:
- 队列为空
- 线程调用1的isEmpty()
- 上下文切换
- 线程2所要求入队(现在的队列不为空)
- 上下文切换
- 线程1不调用lock.wait()事件虽然队列不为空

当然这个问题将得到解决当呼叫等待()/通知()将被放置在一个同步块中。



文章来源: Why does this code result in illegalMonitorState exception?