This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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"
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...
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.
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".
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.
Joined: Sep 03, 2007
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............
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.