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


Groups > comp.lang.java.programmer > #7777 > unrolled thread

Java thread

Started byJack <junw2000@gmail.com>
First post2011-09-10 21:03 -0700
Last post2011-09-11 08:32 -0700
Articles 9 — 7 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#7777 — Java thread

FromJack <junw2000@gmail.com>
Date2011-09-10 21:03 -0700
SubjectJava thread
Message-ID<128ea2e0-7a2e-4c86-9507-8e15a0b892b9@p25g2000pri.googlegroups.com>
For Java thread, how to ensure that wait() does not miss notify()?
Otherwise the thread will hang and can not shut down.

Thanks. Jack

[toc] | [next] | [standalone]


#7778

Frommarkspace <-@.>
Date2011-09-10 21:34 -0700
Message-ID<j4hdpg$ggd$1@dont-email.me>
In reply to#7777
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>


[toc] | [prev] | [next] | [standalone]


#7782

FromPatricia Shanahan <pats@acm.org>
Date2011-09-10 22:42 -0700
Message-ID<UoudnVs6Zbf11_HTnZ2dnUVZ_uCdnZ2d@earthlink.com>
In reply to#7777
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

[toc] | [prev] | [next] | [standalone]


#7783

FromJack <junw2000@gmail.com>
Date2011-09-10 22:48 -0700
Message-ID<0d62ad20-0081-4bf2-9c5d-ee4600423150@f24g2000prb.googlegroups.com>
In reply to#7777
On Sep 10, 9:03 pm, Jack <junw2...@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#7784

FromOwen Jacobson <angrybaldguy@gmail.com>
Date2011-09-11 03:07 -0400
Message-ID<2011091103071380687-angrybaldguy@gmailcom>
In reply to#7783
On 2011-09-11 05:48:28 +0000, Jack said:

> On Sep 10, 9:03 pm, Jack <junw2...@gmail.com> 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




[toc] | [prev] | [next] | [standalone]


#7808

FromLew <lewbloch@gmail.com>
Date2011-09-11 09:37 -0700
Message-ID<eaec41ae-84c0-4e65-a0c3-445562e4b01f@glegroupsg2000goo.googlegroups.com>
In reply to#7783
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

[toc] | [prev] | [next] | [standalone]


#7821

FromArne Vajhøj <arne@vajhoej.dk>
Date2011-09-11 16:56 -0400
Message-ID<4e6d208f$0$308$14726298@news.sunsite.dk>
In reply to#7783
On 9/11/2011 1:48 AM, Jack wrote:
> On Sep 10, 9:03 pm, Jack<junw2...@gmail.com>  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

[toc] | [prev] | [next] | [standalone]


#7795

FromArne Vajhøj <arne@vajhoej.dk>
Date2011-09-11 09:34 -0400
Message-ID<4e6cb8d0$0$313$14726298@news.sunsite.dk>
In reply to#7777
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

[toc] | [prev] | [next] | [standalone]


#7804

FromRoedy Green <see_website@mindprod.com.invalid>
Date2011-09-11 08:32 -0700
Message-ID<cvkp675fsg7eo5aoplvbtf8cc8r1lo2cub@4ax.com>
In reply to#7777
On Sat, 10 Sep 2011 21:03:24 -0700 (PDT), Jack <junw2000@gmail.com>
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) 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web