Java thread

J

Jack

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

Thanks. Jack
 
J

Jack

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
 
O

Owen Jacobson

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
 
A

Arne Vajhøj

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
 
R

Roedy Green

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)
 
L

Lew

Jack said:
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.
 
A

Arne Vajhøj

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top