Condition vs wait notify mechanism

前端 未结 5 1683
遇见更好的自我
遇见更好的自我 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:40

    The biggest problem is that wait/notify is error prone for new developers. The main problem is not knowing how to handle them correctly can result is obscure bug.

    • if you call notify() before wait() it is lost.
    • it can be sometimes unclear if notify() and wait() are called on the same object.
    • There is nothing in wait/notify which requires a state change, yet this is required in most cases.
    • wait() can return spuriously

    Condition wraps up this functionality into a dedicated component, however it behaves much the same.

    There is a question regarding wait/nofity posted minutes before this one and many, many more Search [java]+wait+notify

提交回复
热议问题