Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #7808

Re: Java thread

From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Java thread
Date 2011-09-11 09:37 -0700
Organization http://groups.google.com
Message-ID <eaec41ae-84c0-4e65-a0c3-445562e4b01f@glegroupsg2000goo.googlegroups.com> (permalink)
References <128ea2e0-7a2e-4c86-9507-8e15a0b892b9@p25g2000pri.googlegroups.com> <0d62ad20-0081-4bf2-9c5d-ee4600423150@f24g2000prb.googlegroups.com>

Show all headers | View raw


Jack wrote:
>> For Java thread, how to ensure that wait() does not miss notify()?
>> Otherwise the thread will hang and can not shut down.
> 
> 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.

That is not accurate. You failed to synchronize (using the 'synchronized' keyword) on a monitor, so the call to 'wait()' is not legitimate.  

Also, when the program shuts down, it cannot hang, because it has shut down.

Read the documentation:
<http://download.oracle.com/javase/7/docs/api/java/lang/Object.html#wait(long)>
_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 _Java 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 programming prior to Java 5 will not help.

<http://java.sun.com/docs/books/effective/>
<http://jcip.net/>

Read the documentation.  

-- 
Lew

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Java thread Jack <junw2000@gmail.com> - 2011-09-10 21:03 -0700
  Re: Java thread markspace <-@.> - 2011-09-10 21:34 -0700
  Re: Java thread Patricia Shanahan <pats@acm.org> - 2011-09-10 22:42 -0700
  Re: Java thread Jack <junw2000@gmail.com> - 2011-09-10 22:48 -0700
    Re: Java thread Owen Jacobson <angrybaldguy@gmail.com> - 2011-09-11 03:07 -0400
    Re: Java thread Lew <lewbloch@gmail.com> - 2011-09-11 09:37 -0700
    Re: Java thread Arne Vajhøj <arne@vajhoej.dk> - 2011-09-11 16:56 -0400
  Re: Java thread Arne Vajhøj <arne@vajhoej.dk> - 2011-09-11 09:34 -0400
  Re: Java thread Roedy Green <see_website@mindprod.com.invalid> - 2011-09-11 08:32 -0700

csiph-web