This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes another question from nikos Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "another question from nikos" Watch "another question from nikos" New topic

another question from nikos

Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521

why is the output even coming? i thought after wait there is no one to notify the thread to continue

scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later" someone unknown
Charles Chikito
Ranch Hand

Joined: May 22, 2009
Posts: 76
Can't figure out.

Btw, what does thread.wait() at line 18 actually means. Is it about asking the main thread to wait for thread created. If so, can the main thread be asked to wait for other thread?



Martin Sergeant

Joined: May 20, 2003
Posts: 24
I don't quite understand what is going on
Calling thread.wait() will cause the main thread to pause until another thread calls thread.notify()
As no other thread does, you would expect the program to hang
This is indeed the case if wait is called on a another object

will cause the program to hang

A clue to what is going on, however is that 999999 is always the output, so the main
thread does wait but only until the Job thread has completed.
Therefore maybe when thread has run its course it may call notify on itself?

Charles Chikito
Ranch Hand

Joined: May 22, 2009
Posts: 76
Therefore maybe when thread has run its course it may call notify on itself?

Notify on itself or main, because I guess main is waiting for thread

Correct me if I'm wrong.


Henry Wong

Joined: Sep 28, 2004
Posts: 20136

What you are seeing is an implementation detail of the join() method, and is not guarantee to always behave like this.

The way join() is implemented, is that it checks to see if the joing thread is alive, and if it is still alive, it will call wait() on the Thread object for the joining thread. To make this work, one of the actions that is done, when a thread terminates is that it calls notifyAll() on the thread object... so if you also call wait() on the thread object, just like the join() method, you too, will receive a notification.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: another question from nikos
It's not a secret anymore!