This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Grokking Bitcoin and have Kalle Rosenbaum on-line!
See this thread for details.
Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Suspected Error in Answer for a Thread Question in Practice Exams Java  RSS feed

 
Ranch Hand
Posts: 224
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

That question got me too. I think it's because the assignment of integer values is atomic, so that line 8 always executes as one instruction. Doesn't seem intuitive though, as that line is both reading a variable and writing it again.

In the real exam, we are told how many answers are correct, so we'd have to make a best guess and assume D is not right. However, it's a pain not really understanding why D is incorrect.

I got the question wrong cos I missed the static/non-static bit.

cheers, john
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think, this is because of int values assignment is atomic. So that line 8 always executes as one instruction.So this programme does not produce output "P Q P Q".

But, it may produce the output "R S R S", because when two threads execute, whichever thread that executes first will set id=0 and may be preempted, allowing other thread to start.
Then second thread will set id=1. After that, in whatever order these two threads excute, there is no way to set id again to 0. So id=1 will remain. Therefore both threads will execute the
release() method.Since it is synchronized on object st, one thread has to wait until the other releases lock. So, output will be "R S R S". Because of this, I think we cannot say that there
is no chance for the same method to be executed twice
.
.

Correct me if I am wrong.

 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!