This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please explain this problem from k&b Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please explain this problem from k&b" Watch "Please explain this problem from k&b" New topic
Author

Please explain this problem from k&b

Stefan Koev
Greenhorn

Joined: Oct 01, 2011
Posts: 2
This is a question from the k&b practice exams book and has me confused. The correct answers are ABC according to the answer key. I understand those, but to me, D also seems a valid answer. The book says that D is incorrect because the value of id swaps between 0 and 1, and there is no chance for the same method to be executed twice.
But what if one thread modifies id, and the other thread stills sees the old value of id because id is not volatile? Then both threads could call the pick method, one after the other. Please advise.

public class Stone implements Runnable {
static int id = 1;
public void run() {
try {
id = 1 - id;
if(id == 0) { pick(); } else { release(); }
} catch(Exception e) { }
}
private static synchronized void pick() throws Exception {
System.out.print("P ");
System.out.print("Q ");
}
private synchronized void release() throws Exception {
System.out.print("R ");
System.out.print("S ");
}
public static void main(String[] args) {
Stone st = new Stone();
new Thread(st).start();
new Thread(st).start();
} }


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.
nitin sethi
Ranch Hand

Joined: Jul 14, 2011
Posts: 59

Stefan Koev wrote:But what if one thread modifies id, and the other thread stills sees the old value of id because id is not volatile? Then both threads could call the pick method, one after the other. Please advise.


The variable id is static. So, the two threads would read the present value irrespective of which one changes it. The below link would be a good read for you.

http://www.coderanch.com/t/382150/java/java/volatile-static-keywords

-- Nitin
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Good discussion of this problem here:

http://www.coderanch.com/t/531921/java-programmer-SCJP/certification/Threads-found-OCP-Java-Practice#2412129
Stefan Koev
Greenhorn

Joined: Oct 01, 2011
Posts: 2


Great discussion, thanks!
 
jQuery in Action, 2nd edition
 
subject: Please explain this problem from k&b
 
Similar Threads
Doubt regarding Threads found in K&B OCP Java 6 Practice Exams Book
Threads question from K&B Practice Exams
Suspected Error in Answer for a Thread Question in "Practice Exams Java"
Suspected Error in Answer for a Thread Question in Practice Exams Java
Threads - static variable and static/non static method execution