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!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: Java thread Date: Sun, 11 Sep 2011 09:37:14 -0700 (PDT) Organization: http://groups.google.com Lines: 41 Message-ID: References: <128ea2e0-7a2e-4c86-9507-8e15a0b892b9@p25g2000pri.googlegroups.com> <0d62ad20-0081-4bf2-9c5d-ee4600423150@f24g2000prb.googlegroups.com> Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 24.6.43.78 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1315759125 29820 127.0.0.1 (11 Sep 2011 16:38:45 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 11 Sep 2011 16:38:45 +0000 (UTC) In-Reply-To: <0d62ad20-0081-4bf2-9c5d-ee4600423150@f24g2000prb.googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=24.6.43.78; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7808 Jack wrote: >> For Java thread, how to ensure that wait() does not miss notify()? >> Otherwise the thread will hang and can not shut down. >=20 > For example in the following code: >=20 > while(true) > { > doSomeThing(); //Do some operation once a day > wait(24 * 3600); //sleep for one day. LINE1 > } >=20 > The thread blocks at LINE1 for one day. When the program shuts down, > if the notify() is missing, then the program will hang. That is not accurate. You failed to synchronize (using the 'synchronized' k= eyword) on a monitor, so the call to 'wait()' is not legitimate. =20 Also, when the program shuts down, it cannot hang, because it has shut down= . Read the documentation: _et seq_ The Javadocs also recommend reading Josh Bloch's _Effective Java_ Use the = latest edition; the Javadocs' recommendation is old. To that I'll add _Jav= a Concurrency in Practice_ by Brian Goetz, _et al._ Unfortunately I don't find an update to Doug Lea's _Concurrent Programming = in Java_ that takes into account the changes in the Java language since the= 2nd edition was published twelve years ago. Any book on concurrent Java p= rogramming prior to Java 5 will not help. Read the documentation. =20 --=20 Lew