Why does this code result in illegalMonitorState exception?

大憨熊 提交于 2019-11-28 05:58:38

问题


The code below is trying to insert a random value into a circular queue and remove it. However, there are some synchronization issues. I know I can use higher level routines and I'm going to do that for production code but I was curious why this does not work ? What am I missing here ?

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 class

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();

    }

}

回答1:


Because java.lang.Object#wait, java.lang.Object#notify, java.lang.Object#notifyAll must be called from synchronized block.

As a solution(need to check) you should put your conditions inside synchronized blocks:

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();
    }

}



回答2:


another problem in this code is that even if isEmpty/isFull return true - until you call the adjacent wait the state of the queue might change.
For example:
- queue is empty
- thread 1 calls isEmpty()
- context switch
- thread 2 calls enqueue (now the queue is not empty)
- context switch
- thread 1 not calls lock.wait() event though the queue is not empty

this problem of course will be solved when calls to wait()/notify() will be placed in a synchronized block.



来源:https://stackoverflow.com/questions/13335367/why-does-this-code-result-in-illegalmonitorstate-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!