Condition vs wait notify mechanism

前端 未结 5 1724
遇见更好的自我
遇见更好的自我 2021-01-29 22:17

What is the advantage of using Condition interface/implementations over the conventional wait notify mechanism? Here I quote the comments written by Doug Lea:

5条回答
  •  不要未来只要你来
    2021-01-29 22:31

    There are many advantages like mentioned above about Condition Interface some important are as follows:

    Condition interface comes with Two extra methods that are:

    1)boolean awaitUntil(Date deadline)throws InterruptedException : Causes the current thread to wait until it is signalled or interrupted, or the specified deadline elapses.

    2)awaitUninterruptibly() : Causes the current thread to wait until it is signalled.

    If the current thread's interrupted status is set when it enters this method, or it is interrupted while waiting, it will continue to wait until signalled. When it finally returns from this method its interrupted status will still be set.

    The above two methods are not present in default monitor that is in object class,in some situations we want to set the deadline for thread to wait then we are able to do that by Condition interface.

    In some situations we don't want thread to be interrupted and want current thread to wait until it is signaled then we can go for awaitUninterruptibly method present in Condition Interface.

    For more information Condition Interface Java Documentation:

    http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/locks/Condition.html#awaitUntil%28java.util.Date%29

提交回复
热议问题