aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "another question from nikos" Watch "another question from nikos" New topic
Author

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"....by 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?

Thanks.

_charles

Martin Sergeant
Greenhorn

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
e.g.

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?

Martin
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.

Thanks

_charles
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18712
    
  40

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: another question from nikos