• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Suspected Error in Answer for a Thread Question in "Practice Exams Java"

 
Ranch Hand
Posts: 249
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Friday I posted an article to the Mock Exam Errata forum asking for an explanation for a published answer to a question in Practice Exams Java by Bert Bates and Kathy Sierra. It really looks to me like the answer is in error. Was that the right forum to post to, or should I have posted my article here or some other place?

Kevin Simonson
 
Ranch Hand
Posts: 72
Android Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kevin,

Could you, please, post more details ? If possible, the questions and options for answer as well
 
Kevin Simonson
Ranch Hand
Posts: 249
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suhas Mandrawadkar wrote:Hi Kevin,

Could you, please, post more details ? If possible, the questions and options for answer as well



The article I posted to the Mock Exam Errata forum in its entirety is:

I'm looking at the PDF version of a book titled Practice Exams Java, with subtitles Questions . In-Depth Answers and OCP Java SE 6 Programmer Pracitce Exams. In parentheses below that second subtitle it says, "Exam 310-065," and the authors are listed as Bert Bates and Kathy Sierra. The purpose of the book appears to be to prepare software engineers to take the SCJP exam, which is what several software engineers at my company are preparing for. Each week we read one chapter from the Sun Certified Programmer for Java 6 Study Guide, by the same authors, and we meet for ninety minutes once that week to go over problems from the practice exams. This week (this Tuesday, in particular), it's my turn to lead the study group for the chapter on threads, so I went through the assessment tests and practice exams looking for problems I could use. One of those was problem #25 of Practice Exam 3. The problem with the answer provided for it, on pages 290 and 291 are (formatted by me for Notepad):

25. Given:

4. public class Stone implements Runnable {
5. static int id = 1;
6. public void run() {
7. try {
8. id = 1 - id;
9. if(id == 0) { pick(); } else { release(); }
10. } catch(Exception e) { }
11. }
12. private static synchronized void pick() throws Exception {
13. System.out.print("P "); System.out.print("Q ");
14. }
15. private synchronized void release() throws Exception {
16. System.out.print("R "); System.out.print("S ");
17. }
18. public static void main(String[] args) {
19. Stone st = new Stone();
20. new Thread(st).start();
21. new Thread(st).start();
22. } }

Which are true? (Choose all that apply.)

A. The output could be P Q R S
B. The output could be P R S Q
C. The output could be P R Q S
D. The output could be P Q P Q
E. The program could cause a deadlock.
F. Compilation fails.


Answer (for Objective 4.3):
[/] A, B, and C are correct. Since pick() is static and release() is
non-static, there are two locks. If pick() was non-static, only A would be
correct.
[X] D is incorrect because line 6 swaps the value of id between 0 and 1. There
is no chance for the same method to be executed twice. E and F are
incorrect based on the above.

A, B, and C I understand just fine. But I don't understand at all the assertion that there "is no chance for the same method to be executed twice." Access to and modification of static variable <id> isn't synchronized on line 8. The two times <run()> is executed, once for each thread, could happen in parallel, couldn't they? What's to keep each thread from reading a 1 value from <id> before either of them write to <id>?

In that case each would write a zero to <id>, which would cause <pick()> to be executed twice, which would result in output "P Q P Q". So it looks like D is a correct answer. Am I missing something here?

Kevin Simonson
 
Suhas Mandrawadkar
Ranch Hand
Posts: 72
Android Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Either Thread A will modify "id" first or Thread B will. Both cannot modify it at the same time. Answers B and C are results of threads running in parallel. Else the answer would have been PQRS.
 
Kevin Simonson
Ranch Hand
Posts: 249
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suhas Mandrawadkar wrote:Either Thread A will modify "id" first or Thread B will. Both cannot modify it at the same time. Answers B and C are results of threads running in parallel. Else the answer would have been PQRS.



Is "id = 1 - id;" an atomic statement then? If so, why is it atomic?

It seems to me more likely that this statement will be broken up into at least three steps, (1) reading <id> into a register, (2) subtracting the register from one, and finally (3) storing the result of the subtraction back in <id>.

What's to keep the first thread from reading <id> into one of its registers, and then the second thread interrupting to also read the contents of <id> into one of its registers, and then for each thread to do the subtracts and stores back into <id> in any order at all? In such a case, the value stored each time will be zero, and <pick()> will be called twice, resulting in output "P Q P Q".

The same result would occur if one thread did both steps (1) and (2), and then at that point the second thread interrupted and did step (1), and then the remaining steps followed in any order.

Kevin Simonson
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This very question has been the subject of discussion a number of times. Did you try searching the forum?
 
Kevin Simonson
Ranch Hand
Posts: 249
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Deems wrote:This very question has been the subject of discussion a number of times. Did you try searching the forum?


Dennis, thanks for pointing me to this forum's search mechanism. I did a search on "Practice Exams Java Exam 3 25", and it led me to
"https://coderanch.com/t/531921/java-programmer-SCJP/certification/Threads-found-OCP-Java-Practice", which is a description of this very same controversy. Anybody interested in this question can click on this link and go down to the statements made at the bottom consecutively by Mike Simmons, Dennis himself, and Bert Bates (one of the authors of Practice Exams Java). Bert is "leaning towards thinking that there's a problem with the question," but as of early morning 27 March he hadn't "looked at it closely yet." I just posted a response to him asking him what the last word was on this question.

Kevin Simonson
 
Suhas Mandrawadkar
Ranch Hand
Posts: 72
Android Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, that is indeed an interesting thread to go through.
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic