File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exam Lab Threading question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exam Lab Threading question" Watch "Exam Lab Threading question" New topic
Author

Exam Lab Threading question

Jamie MacDonald
Greenhorn

Joined: May 17, 2008
Posts: 20
This is from Devaka Cooray's Exam Lab (Practice Exam 1, Question 72):

What is result of attempting to compile and run this program?

The "correct" answer given by ExamLab is Prints "ABC". The explanation includes commentary noting that the two synchronized blocks don't interfere with each other because they are synchronized on different objects. While I agree with the commentary, I don't agree with ExamLab as to the correct answer. While "ABC" should be the result almost every time the application is run, it is not guaranteed to be the result. The nature of threading means that the System.out.print(s); statement in XMap.run() is not guaranteed to be reached within 1 second of the thread starting. It probably will, and if you ran it a million times, you still are likely to get "ABC" every time. But the nature of threads is such that you cannot guarantee it. (This is why testing and debugging multi-threaded code is so difficult--just because something happens every time you ran it, doesn't mean it's the only thing that could happen.) However unlikely, it seems to me that the code as written could also result in "XYZ" being printed, if the assignment line in main is reached before the print statement in XMap.run(). Because of this possibility (however remote it may be) I would be inclined to give the answer to the question as "Result is unpredictable"

Can anyone either confirm or refute my argument?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Jamie there was a long discussion on this some time ago and you are right. The output is unpredictable. It is very very unlikely that XYZ will be displayed but there IS a possibility thus for SCJP you'll have to say that the output is unpredictable...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Hi Jamie,

While I don't agree with you and Ankit , please read this topic.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
I have just started the Threads chapter. So I am not sure, but I also feel "Result is unpredictable".
Waiting for confirmation........... :?

SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Sachin Adat wrote:I have just started the Threads chapter. So I am not sure, but I also feel "Result is unpredictable".
Waiting for confirmation........... :?


I think it's better if we continue the discussion on the original topic.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Treimin Clark wrote:I think it's better if we continue the discussion on the original topic.


No, I don't think so. Just discuss this here...
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
I have read both the posts, so no doubt I am fully confused.............
Not about understanding the question or answer .......... but what should I answer in the exam???
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ahhh do not worry Sachin, you will not get such confused question in the exam. If get than it will be mentioned clearly that thread td will run immediately.


SCJP 6
kshitij dogra
Ranch Hand

Joined: Dec 28, 2008
Posts: 39
Hey Ankit

Cool down,

yup I too think, the answer should be what Ankit said. Sometime I too encountered a problem like this from Paul Sanghera's book.

Give me sometime, I will get you that code, so we can discuss upon it too.


SCJP 5.0 - 100%
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Ankit Garg wrote:
Treimin Clark wrote:I think it's better if we continue the discussion on the original topic.


No, I don't think so. Just discuss this here...


It had some valuable comments,... anyway...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Looking at that code, I would say that in principle it is possible that it would print XYZ instead of ABC. (In practice it will normally print ABC). There's no guarantee that the code in the run() method (running in a new thread) will be executed before the s = "XYZ"; in the main() method.

Looking at chapter 17 of the JLS and the API documentation of Thread.start(), there's nothing that says when the code in the thread starts running. (The JLS only specifies in section 17.4.5 that it starts running after the start() method is called, which is ofcourse not surprising).

So, in principle it could take more than one second for the code in the thread to start running. In practice, I can imagine that this is possible on a heavily loaded system - if the computer is so busy that it takes a long time before the thread can be executed.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Hi Jesper,
That was a good explanation.........but it still doesn't answer the question - What to we select as the answer?
Please see the other post also, where they say that you have to answer the question "ABC" even though you are 99% sure and there is still 1% probability of "XYZ".

Punit,
In the other post Devaka said that these type of questions do come up in the exam.............
Devaka Cooray wrote:but remember that there will BE this type questions in the real exam, as I got.


Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Ohhh............he also said this
Devaka Cooray wrote:
On the Sun exam, they given an assumption statement like that, for some questions. But I didn't include that assumption statement in this question, because...

I thought like this; we know that the sleep(long) method takes a duration, which is always higher than to the specified duration to be completed. In this question, I used a statement sleep(1000); so the user can assume it may take 1000 ms, 1001ms, 2000ms or probably 100000ms to complete (where ms=milliseconds). But it is not a problem to this question. Even though that statement takes morethan 30 seconds to be completed, the answer will be "ABC". Because of this, I didn't include the above assumption-statement.

However, now I think it's better to include an assumption statement like that, so it's more easy select an answer. I'll mark this and will include it on the next version. Once the next version is released, you will be notified via JavaRanch. Thanks for helping me to improve this simulator.


I guess this makes it clear, in exams we can expect an assumption statement, which is good enough for selecting "ABC".......
I am OK now............
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3057
    
  38

Sachin Adat wrote:
I guess this makes it clear, in exams we can expect an assumption statement, which is good enough for selecting "ABC".......


Yep


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Chandella Montero
Ranch Hand

Joined: Feb 18, 2011
Posts: 89
Ankit Garg wrote:Jamie there was a long discussion on this some time ago and you are right. The output is unpredictable. It is very very unlikely that XYZ will be displayed but there IS a possibility thus for SCJP you'll have to say that the output is unpredictable...


What a relief to find this! well done for clarifying it for us all, Ankit.


OCA, OCP Java 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exam Lab Threading question