• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Exam Lab Threading question

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jamie,

While I don't agree with you and Ankit , please read this topic.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have just started the Threads chapter. So I am not sure, but I also feel "Result is unpredictable".
Waiting for confirmation........... :?
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Treimin Clark
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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............
 
Sheriff
Posts: 7345
1404
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic