2014-11-01 30 views
0

我得到这个异常,但我不明白为什么。embeddedStateException对嵌套锁

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

线程类:

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

堆栈跟踪:

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

我是在notifyAll()越来越thie例外。执行global.lock()块的线程是所有者,那么为什么我得到这个?

+1

这是什么神秘的例外?我们猜测吗?提供完整的堆栈跟踪。 – 2014-11-01 13:14:50

+0

@BoristheSpider等一下。 – OiRc 2014-11-01 13:15:54

+0

@BoristheSpider更新了 – OiRc 2014-11-01 13:19:02

回答

1

看来你与监视器锁定Object#notifyAll &​​-statements)和条件混淆变量Condition#signalAll & Condition#await)。

如果您在Condition#await上等待,则必须使用Condition#signalAll而不是Object#notifyAll

更新: 请参阅notifyAll() throws IllegalMonitorStateException解决OP的问题。

+0

感谢您的答案,我改变了片段,你能看到更新吗? – OiRc 2014-11-01 15:41:52

+0

我仍然越来越illegalAnonymousMonitorException notifyAll(),你能解释我为什么吗? – OiRc 2014-11-01 15:43:04