How to use wait and notify in Java without IllegalMonitorStateException?

后端 未结 12 2367
后悔当初
后悔当初 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

    we can call notify to resume the execution of waiting objects as

    public synchronized void guardedJoy() {
        // This guard only loops once for each special event, which may not
        // be the event we're waiting for.
        while(!joy) {
            try {
                wait();
            } catch (InterruptedException e) {}
        }
        System.out.println("Joy and efficiency have been achieved!");
    }
    

    resume this by invoking notify on another object of same class

    public synchronized notifyJoy() {
        joy = true;
        notifyAll();
    }
    

提交回复
热议问题