Почему этот код приводит к исключению invalidMonitorState?

Код ниже пытается вставить случайное значение в круговую очередь и удалить его. Однако есть некоторые проблемы с синхронизацией. Я знаю, что могу использовать процедуры более высокого уровня, и я собираюсь сделать это для производственного кода, но мне было любопытно, почему это не работает? Что мне здесь не хватает?

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

// Тестовый класс

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

    }

}

Ответы на вопрос(2)

Ваш ответ на вопрос