Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx01.iad01.newshosting.com!newshosting.com!69.16.185.21.MISMATCH!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe06.iad.POSTED!00000000!not-for-mail From: Owen Jacobson Newsgroups: comp.lang.java.programmer Message-ID: <2011091103071380687-angrybaldguy@gmailcom> References: <128ea2e0-7a2e-4c86-9507-8e15a0b892b9@p25g2000pri.googlegroups.com> <0d62ad20-0081-4bf2-9c5d-ee4600423150@f24g2000prb.googlegroups.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: Java thread User-Agent: Unison/2.1.5 Lines: 34 X-Complaints-To: abuse@UsenetServer.com NNTP-Posting-Date: Sun, 11 Sep 2011 07:07:10 UTC Date: Sun, 11 Sep 2011 03:07:13 -0400 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7784 On 2011-09-11 05:48:28 +0000, Jack said: > On Sep 10, 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. >> >> Thanks. Jack > > For example in the following code: > > while(true) > { > doSomeThing(); //Do some operation once a day > wait(24 * 3600); //sleep for one day. LINE1 > } > > The thread blocks at LINE1 for one day. When the program shuts down, > if the notify() is missing, then the program will hang. The wait/notify protocol is a synchronization tool for collaborating threads, not a general-purpose delay mechanism. You probably want a timer (ScheduledExecutor or java.util.Timer or something) to handle once-an-interval events like this. If you insist on tying up an entire thread's worth of memory and OS resources on doing nothing, use Thread.sleep instead, and issue shutdown notifications using Thread.interrupt as markspace suggested. -o