Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx01.iad01.newshosting.com!newshosting.com!69.16.185.16.MISMATCH!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!news2.google.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sun, 11 Sep 2011 00:43:04 -0500 Date: Sat, 10 Sep 2011 22:42:25 -0700 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows NT 5.2; WOW64; rv:6.0.2) Gecko/20110902 Thunderbird/6.0.2 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Java thread References: <128ea2e0-7a2e-4c86-9507-8e15a0b892b9@p25g2000pri.googlegroups.com> In-Reply-To: <128ea2e0-7a2e-4c86-9507-8e15a0b892b9@p25g2000pri.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 19 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 70.230.201.107 X-Trace: sv3-89tVstsOor0hzzfp191jWwqs4bwWHb7/ZCj/jw8VJlKsBCT8+axhbVfF2ZQgV10uYt2x3tqtBCi08kI!JVHx21WdUXKhdvR+2Q1J/+TtXqfbv4B7PqGIqbJ9YJ0eqQ+jkFgyyfB9ohGDfnm9eHO0d4rTi40I!pbfdPb/+gDKUkI15h2d3n4CE2ggIHM8K15Q+5MSsxvnDXQg= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2023 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7782 On 9/10/2011 9:03 PM, Jack wrote: > For Java thread, how to ensure that wait() does not miss notify()? > Otherwise the thread will hang and can not shut down. I'm not quite sure what you are asking, but here are a couple of possible answers - if neither fits, consider a more detailed question. 1. wait and notify can only be called in synchronized code. The wait code should check that it is supposed to wait inside the same synchronized block. The notify code should only change whatever controls whether to wait inside the same synchronized block as the notify. 2. If you are using a current version of Java, check the java.util.concurrent package. A lot of things that used to be done with explicit wait-notify code can instead be done using concurrent queues, barriers etc. Patricia