Software development is an exercise in thinking not coding.
SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Originally posted by Tomasz Kalkosinski:
Hello Ranchers. I've came to this question on Sun's ePractice mock exam:
Answers:
A The output can never contain the value 10. B The output can never contain the value 30. C The output can never contain the value 297. D The output can never contain the value 820. E Making the run method un-synchronized will NOT change the possible output.
E is marked as a correct one, but I have doubts. I've answered C - run () is synchronized so as one thread enters it no other can enter. Hence i will be increased 10 for each thread. 297 is not possible.
Another doubt is about unsynchronizing run. E claims that output will not change. In my opinion it can actually change. Put some sleep () before yield. While some thread is inside counting to 10 and gracefully (yield) lets other thread to enter - that thread does i++ (because i % 10 != 0) and then counts its own. So they do come concurrent i increments.
Situation complicates - when one yields it never knows when is coming back to work. So... is 297 possible this way?
Or maybe am I wrong somewhere? I've played with this question, added threads, counter and sleep and still there is no clear answer for me. I hope you Ranchers can help me.
SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Originally posted by Tomasz Kalkosinski:
Ninad Kulkarni: you didn't hurt my feelings. I just don't want to introduce confusion about what question we discuss here.
Also I think you shouldn't consider program's output based on your running programs. You should provide logical deduction.
I have doubts about yield () method when x ==4, because it's unpredictable when run () isn't synchronized.
I also have doubts about for (int x = 0 ; x < 10 ; x++, i++). Try to increase 10 to 10000 and run some tests on unsychronized run(). Just because 10 is small you can't assume that on other machine, on other JVM counting from 0 to 9 would be the same. Maybe on your machine it's pretty fast, but on other JVM would take turns on threads every time it loops? It is hard when you think about it.
SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Originally posted by Tomasz Kalkosinski:
I think Ninad that option E doesn't say: output is unpredictable either run is synchronized or not. It says that output doesn't change. Actually it does.
I've used your NoGo1 class. Results, synchronized run ():
Results, normal - unsynchronized run ():
Other unsychronized run ():
Did you mention 34? And three 0s? What can you say about it?
SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Consider Paul's rocket mass heater. |