Professional Documents
Culture Documents
Java FAQ
Recent Topics
Register / Login
A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal
Author
Raju Champaklal Ranch Hand Joined: Dec 10, 2009 Posts: 521
view plain
c opy to c lipboard
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 . 2 2 .
www.coderanch.com/t/476444/java-programmer-SCJP/certification/nikos
6/12/13
2 3 .
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
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
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.
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 .
I n t e g e ri=n e wI n t e g e r ( 1 0 ) ; t h r e a d . s t a r t ( ) ; s y n c h r o n i z e d( i ){ i . w a i t ( ) ; }
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
www.coderanch.com/t/476444/java-programmer-SCJP/certification/nikos
2/3
6/12/13
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: 16779
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
19
I like...
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Similar Threads Thread question Question on Thread/sychronized Misunderstanding with the wait() method. Problem in Threading - 2 join method
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 12, 2013 11:17:17 .
www.coderanch.com/t/476444/java-programmer-SCJP/certification/nikos
3/3