You are on page 1of 5

5/4/13

a Thread Question from Nikos Blog [Solved] (SCJP forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author

a Thread Question from Nikos Blog


posted 1/27/2010 11:50:32 PM

saima kanwal Ranch Hand Joined: Oct 05, 2009 Posts: 148

Consider the following code

qs 66 from Nikos Blog "Threads")

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 .

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 ;


1/5

www.coderanch.com/t/480435/java-programmer-SCJP/certification/Thread-Nikos-Blog

5/4/13

a Thread Question from Nikos Blog [Solved] (SCJP forum at JavaRanch)

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.

SC JP 6.0 (86%) SC WC D 5 (94%)

Neha Daga Ranch Hand Joined: Oct 30, 2009 Posts: 504

posted 1/28/2010 1:06:03 AM

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

a Thread Question from Nikos Blog [Solved] (SCJP forum at JavaRanch)

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

posted 1/28/2010 1:31:09 AM


Neha Daga wrote:

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

posted 1/28/2010 2:54:50 AM

We are not calling notify method then how can it notify all threads ? Can you please explain a bit more?

Neha Daga Ranch Hand

posted 1/28/2010 5:25:07 AM


3/5

www.coderanch.com/t/480435/java-programmer-SCJP/certification/Thread-Nikos-Blog

5/4/13

a Thread Question from Nikos Blog [Solved] (SCJP forum at JavaRanch)

Joined: Oct 30, 2009 Posts: 504

saima kanwal wrote:

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

posted 1/28/2010 5:47:10 AM

thanks neha , now i got it.

Joss Armstrong Greenhorn Joined: Jul 29, 2004 Posts: 16

posted 1/28/2010 3:53:09 PM

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.

subject: a Thread Question from Nikos Blog

www.coderanch.com/t/480435/java-programmer-SCJP/certification/Thread-Nikos-Blog

4/5

5/4/13

a Thread Question from Nikos Blog [Solved] (SCJP forum at JavaRanch)

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 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/480435/java-programmer-SCJP/certification/Thread-Nikos-Blog

5/5

You might also like