the difference between notify and notifyAll
1 notify is wake the thread which invoke the wait-method
2 notifyAll is wake all the threads waiting for the lock released
3 why can form the thread invoking the wait-method and threads waiting for the lock released?
we know, if a synchronized method is invoked, all the other synchronized methods belong to the object can not be invoked, so the invoking threads must be blocked, and waiting the method can be invoke, so they all waite for the lock released in the list of waiting queue.
a synchronized class is the same;
when the notify method occurs, the waiting one who invoked wait() be wake;
when the notifyAll method occurs, the waiting queue choose the most prior one to wake, can be the one invoking wait(), also can be the ones waiting the lock released;
1 notify is wake the thread which invoke the wait-method
2 notifyAll is wake all the threads waiting for the lock released
3 why can form the thread invoking the wait-method and threads waiting for the lock released?
we know, if a synchronized method is invoked, all the other synchronized methods belong to the object can not be invoked, so the invoking threads must be blocked, and waiting the method can be invoke, so they all waite for the lock released in the list of waiting queue.
a synchronized class is the same;
when the notify method occurs, the waiting one who invoked wait() be wake;
when the notifyAll method occurs, the waiting queue choose the most prior one to wake, can be the one invoking wait(), also can be the ones waiting the lock released;
本文详细解释了Java中synchronized块中的notify与notifyAll方法的区别。notify只会唤醒调用wait方法的那个线程;而notifyAll则会唤醒所有等待锁释放的线程。文章进一步解释了为什么会有线程调用wait方法以及哪些线程在等待锁释放。
373

被折叠的 条评论
为什么被折叠?



