`
niwtsew
  • 浏览: 70796 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

why wait() always in a loop

阅读更多

As we know ,jdk API doc suggests to use wait() always in a loop as below:

 As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait();
         ... // Perform action appropriate to condition
     }

 

But what is a spurious wakeup? two understandings which I think both make sense.

1. Sometimes, one might have several threads waiting on the same object. Those threads may be interested in different aspects of that object.   Say one thread does a notifyAll() on an object, to notify that there have been changes to one particular aspect. All waiting threads will wake up, but only some of  them will be interested in the aspect that has changed; the remainder will have experienced a "spurious wake-up".

2. For inexplicable reasons it is possible for threads to wake up even if notify() and notifyAll() has not been called. This is known as spurious wakeups. Wakeups without any reason.

 

And we can also find some information in Java Language Spec:

 

An internal action by the implementation. Implementations are permitted,
although not encouraged, to perform ``spurious wake-ups'' -- to remove
threads from wait sets and thus enable resumption without explicit instructions
to do so. Notice that this provision necessitates the Java coding practice
of using wait only within loops that terminate only when some logical
condition that the thread is waiting for holds.

分享到:
评论

相关推荐

    C# 7.0 in A Nutshell

    Since its debut in 2000, C# has become a language of unusual flexibility and breadth, but its continual growth means there’s always more to learn. Organized around concepts and use cases, this ...

    A Practical Guide to Designing with Data.pdf

    There is a new vernacular in the realm of data representation, but that doesnʼt mean we should ignore the underlying principles and best practices of humble charts and graphs. Once you have ...

    Algorithms in a Nutshell

    Each major algorithm is presented in the style of a design pattern that includes information to help you understand why and when the algorithm is appropriate. With this book, you will: Solve a ...

    C# 7.0 in a Nutshell: The Definitive Reference -epub

    Since its debut in 2000, C# has become a language of unusual flexibility and breadth, but its continual growth means there’s always more to learn. Organized around concepts and use cases, this ...

    Why Eve and Mallory Love Android:An Analysis of Android SSL(In) Security

    文章标题为:“Why Eve and Mallory Love Android:An Analysis of Android SSL(In)Security”,从标题我们可以看出,本文将重点分析Android操作系统中SSL/TLS协议安全性的潜在问题,以及这些安全漏洞为何受到攻击者...

    A Problems Based Course in Advanced Calculus

    Some years ago at a national meeting of mathematicians, many of the ...or you can wait until one of the words crops up in your reading, and learn the concepts when you need them. xvii 英文原版 超清

    Flexbox.in.CSS.2017.5.pdf

    Layout designers rejoice: now you can greatly simplify the task of laying out your web page or application with Flexbox, the CSS Flexible ... Why wait? Learn how to make your web pages come alive today.

    Docker-in-Action.pdf

    see problems that they experienced daily solved in a matter of moments. Those peo- ple reacted with an excitement similar to mine. In the summer of 2014, an associate publisher with Manning called me ...

    The book of Why中文版1

    《The Book of Why 中文版1》是一本深入探讨因果关系科学的著作,作者通过丰富的实例和理论,引领读者理解并掌握如何从数据中提取出真正的因果信息。书中的核心概念是“因果关系之梯”,它揭示了因果推理的三个不同...

    C# 6.0 in a Nutshell(O'Reilly,2015)

    When you have questions about C# 6.0 or the .NET CLR and its core Framework assemblies, this bestselling ... Dive in and discover why this Nutshell guide is considered the definitive reference on C#.

    Optical Lithography: Here is Why

    Readers will also get a primer on the outlook of optical lithography and the many next-generation technologies that may greatly enhance semiconductor manufacturing in the near future. Topics ...

    Writing.An.Interpreter.In.Go.epub

    In this book we will create a programming language together. We'll start with 0 lines of code and end up with a fully working interpreter for the Monkey* programming language. Step by step. From ...

    Open Disk Imager in a Nutshell

    ODIN is a utility for easy backup of hard drive ... Why spend money for a commercial solution? ODIN supports snapshots can be run from command line or with a GUI and runs on 32-Bit and 46-Bit operating

    外研版一起小学英语六下《Module 8Unit 2 Why are you wearing a hat》word教案.docx

    【外研版一起小学英语六下《Module 8 Unit 2 Why are you wearing a hat》】 本教案旨在帮助小学六年级的学生掌握英语中使用"Why"询问原因并用"Because"来回答的技巧。课程分为三个课时进行,涵盖词汇学习、对话...

Global site tag (gtag.js) - Google Analytics