How to use wait and notify in Java without IllegalMonitorStateException?

后端 未结 12 2127
后悔当初
后悔当初 2020-11-22 02:21

I have 2 matrices and I need to multiply them and then print the results of each cell. As soon as one cell is ready I need to print it, but for example I need to print the [

12条回答
  •  误落风尘
    2020-11-22 03:16

    To be able to call notify() you need to synchronize on the same object.

    synchronized (someObject) {
        someObject.wait();
    }
    
    /* different thread / object */
    synchronized (someObject) {
        someObject.notify();
    }
    

提交回复
热议问题