Java thread

Discussion in 'Java' started by Jack, Sep 11, 2011.

  1. Jack

    Jack Guest

    For Java thread, how to ensure that wait() does not miss notify()?
    Otherwise the thread will hang and can not shut down.

    Thanks. Jack
     
    Jack, Sep 11, 2011
    #1
    1. Advertising

  2. Jack

    markspace Guest

    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.



    You can't. The timing involved can always be such that notify is called
    when the thread is not waiting.

    I believe you're using the wrong mechanism if you're worried about
    thread shutdown. Look into Thread.interrupt() instead.

    <http://download.oracle.com/javase/tutorial/essential/concurrency/interrupt.html>
     
    markspace, Sep 11, 2011
    #2
    1. Advertising

  3. Jack

    Jack Guest

    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.

    Jack
     
    Jack, Sep 11, 2011
    #3
  4. 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
     
    Owen Jacobson, Sep 11, 2011
    #4
  5. Jack

    Arne Vajhøj Guest

    On 9/11/2011 12:03 AM, Jack wrote:
    > For Java thread, how to ensure that wait() does not miss notify()?
    > Otherwise the thread will hang and can not shut down.


    Try post a description of what you want to achieve and the
    code you have now and the problems you observe.

    Usually it is sufficient and certainly easier to use
    libraries (like java.util.concurrent package) than
    messing around with wait and notify.

    Arne
     
    Arne Vajhøj, Sep 11, 2011
    #5
  6. Jack

    Roedy Green Guest

    On Sat, 10 Sep 2011 21:03:24 -0700 (PDT), Jack <>
    wrote, quoted or indirectly quoted someone who said :

    >For Java thread, how to ensure that wait() does not miss notify()?
    >Otherwise the thread will hang and can not shut down.


    Ordinary programmers almost never use wait/notify directly, unless
    they were experimenting to understand them. Instead, you rely on code
    written my Doug Lea and other experts. It is so easy for mortals to
    write code that works only most of the time. See
    http://mindprod.com/jgloss/timer.html
    http://mindprod.com/jgloss/concurrent.html
    http://mindprod.com/jgloss/thread.html


    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
    the search for a superior moral justification for selfishness.
    ~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
     
    Roedy Green, Sep 11, 2011
    #6
  7. Jack

    Lew Guest

    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 the2nd 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
     
    Lew, Sep 11, 2011
    #7
  8. Jack

    Arne Vajhøj Guest

    On 9/11/2011 1:48 AM, Jack wrote:
    > 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.


    Looks like an excellent chance to avoid wait and use
    Thread.sleep !

    Arne
     
    Arne Vajhøj, Sep 11, 2011
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Terminating a thread from the main thread

    Charles A. Lackman, Dec 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,568
    Herfried K. Wagner [MVP]
    Dec 9, 2004
  2. pawo
    Replies:
    0
    Views:
    517
  3. Stephen Miller
    Replies:
    3
    Views:
    3,998
    Stephen Miller
    Jul 2, 2004
  4. Johanna
    Replies:
    0
    Views:
    616
    Johanna
    Oct 13, 2004
  5. =?Utf-8?B?Sm9oYW5uYQ==?=
    Replies:
    3
    Views:
    1,972
    =?Utf-8?B?Sm9oYW5uYQ==?=
    Oct 15, 2004
Loading...

Share This Page