Professional Documents
Culture Documents
Java FAQ
Recent Topics
Register / Login
Author
saima kanwal Ranch Hand Joined: Oct 05, 2009 Posts: 148
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 .
www.coderanch.com/t/480435/java-programmer-SCJP/certification/Thread-Nikos-Blog
5/4/13
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 .
} } 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 ( ) ) ; } }
Answer choices: 1. It prints 0. 2. It prints 999999. 3. The output is not guaranteed to be any of the above.
According to me the answer should be : the main would wait there for ever because of the absence of notify() from class Job 's run method but the answer as given by the author is 2. that is it prints 999999. Is he making use of the same concept of implementation detail that is a thread when completes , calls notifyAll implicitly. Is that the reason of this answer ? or i m missing something else? If yes , then again i m confused because we should not consider this implementation detail fact while solving questions as it is only dependent on particular JVM and behaves differently on others.
Neha Daga Ranch Hand Joined: Oct 30, 2009 Posts: 504
I cant say about implementation details. But my strategy to solve these kind of questions was:
www.coderanch.com/t/480435/java-programmer-SCJP/certification/Thread-Nikos-Blog
2/5
5/4/13
when the job thread has started it will take some time to complete its job i.e the run method and before it does that the main thread will go into wait as its the next line in the program. So, when the run method will complete it will notify all threads and the main thread will be notified. If the run method had nothing much to do then I would say output cannot be determined because in that case whichever thread gets control executes first. I dont know if I am correct, but I got all the answers correct with this strategy.
SC JP 1.6 96%
saima kanwal Ranch Hand Joined: Oct 05, 2009 Posts: 148
If the run method had nothing much to do then I would say output cannot be determined because in that case whichever thread gets control executes first.
Thnaks neha , i understand your explanation but could not understand what you meant by saying the above "run method had nothing much to do". Please explain . It will help me and i am worried these days about these confusing threads because my test date is near.
Harpreet Singh janda Ranch Hand Joined: Jan 14, 2010 Posts: 317
I like...
We are not calling notify method then how can it notify all threads ? Can you please explain a bit more?
www.coderanch.com/t/480435/java-programmer-SCJP/certification/Thread-Nikos-Blog
5/4/13
Thnaks neha , i understand your explanation but could not understand what you meant by saying the above "run method had nothing much to do". Please explain . It will help me and i am worried these days about these confusing threads because my test date is near.
What I meant is when run method does not have a job which will take a while to complete. like if run method as a SOP statement it has nothing much to do and if the newly started thread gets control it may complete its run method and make the implicit notifyAll call before the main thread gets the control back. So in this case the ouyput cant be determined as its completely dependent on jvm that which thread will execute first.
saima kanwal Ranch Hand Joined: Oct 05, 2009 Posts: 148
Because of the implementation detail of the JVM described by Henry Wong here http://www.coderanch.com/t/480030/Programmer-Certification-SCJP/certification/with-wait-notify the answer will be 999999 (at least in some JVMs) because the object that you are synchronized on is a Thread subclass and termination of a thread implicity calls notifyAll()
SC JP 6, OC PWC D JEE5, OC EWSD JEE6
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
www.coderanch.com/t/480435/java-programmer-SCJP/certification/Thread-Nikos-Blog
4/5
5/4/13
Similar Threads Threads -Niko-Blog Question 66 Question on Thread/sychronized notify() question from Nikos Thread-doubt on wait Simple Thread Program : Need Clarification
All times above are in your local time zone & format.T he current ranch time (not your local time) is May 04, 2013 06:33:37 .
www.coderanch.com/t/480435/java-programmer-SCJP/certification/Thread-Nikos-Blog
5/5