You are on page 1of 3

6/12/13

another question from nikos (SCJP forum at JavaRanch)

Big Moose Saloon


A friendly place for programming greenhorns!
Search

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

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author
Raju Champaklal Ranch Hand Joined: Dec 10, 2009 Posts: 521

another question from nikos


posted 12/28/2009 2:51:23 AM

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

c l a s sJ o be x t e n d sT h r e a d{ p r i v a t eI n t e g e rn u m b e r=0 ; p u b l i cv o i dr u n ( ){ f o r( i n ti=1 ;i<1 0 0 0 0 0 0 ;i + + ){ n u m b e r + + ; } } p u b l i cI n t e g e rg e t N u m b e r ( ){ r e t u r nn u m b e r ; } } p u b l i cc l a s sT e s t{ p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ) t h r o w sI n t e r r u p t e d E x c e p t i o n{ J o bt h r e a d=n e wJ o b ( ) ; t h r e a d . s t a r t ( ) ; s y n c h r o n i z e d( t h r e a d ){ t h r e a d . w a i t ( ) ; } S y s t e m . o u t . p r i n t l n ( t h r e a d . g e t N u m b e r ( ) ) ; } }


1/3

www.coderanch.com/t/476444/java-programmer-SCJP/certification/nikos

6/12/13

another question from nikos (SCJP forum at JavaRanch)

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

Charles Chikito Ranch Hand Joined: May 22, 2009 Posts: 76

posted 12/28/2009 6:42:53 AM

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

posted 12/28/2009 7:51:13 AM

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

another question from nikos (SCJP forum at JavaRanch)

Charles Chikito Ranch Hand Joined: May 22, 2009 Posts: 76

posted 12/28/2009 9:00:20 AM

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

posted 12/28/2009 9:08:52 AM

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)

I agree. Here's the link: http://aspose.com/file-tools

subject: another question from nikos

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 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/476444/java-programmer-SCJP/certification/nikos

3/3

You might also like