0

i' m getting this exception but i don't understand why.

public Test() {
        globalLock = new ReentrantLock();
        condition = globalLock.newCondition();
    }

public void increaseRow(Integer row) {
    matrixLock.lock();
    try {
        while (countIncreasingColumn > 0)
            condition.await();
        countIncreasingRow++;
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
       condition.notifyAll();
        matrixLock.unlock();
        synchronized (rows.get(row)) {
            for (int j = 0; j < column; j++)
                matrix[row][j] += 1;
            countIncreasingRow--;
        }
    }
}

thread class:

public void run() {
        while (true) {
            test.function(new Random().nextInt(10));
        }
    }

stackTrace:

Exception in thread "Thread-0" waiting thread for test 18
java.lang.IllegalMonitorStateException
    at java.lang.Object.notifyAll(Native Method)

i' m getting thie exception on notifyAll(). The thread that execute the global.lock() block is the owner,so why i'm getting this?

OiRc
  • 1,602
  • 4
  • 21
  • 60

1 Answers1

1

It seems you are confused with Monitor Lock(Object#notifyAll & synchronized-statements) and Condition Variable(Condition#signalAll & Condition#await).

If you wait on Condition#await, you must use Condition#signalAll instead of Object#notifyAll.

UPDATE: Please see notifyAll() throws IllegalMonitorStateException to solve OP's question.

Community
  • 1
  • 1
yohjp
  • 2,985
  • 3
  • 30
  • 100