Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I doubt with question about Threads from SCJP Practice Exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I doubt with question about Threads from SCJP Practice Exam" Watch "I doubt with question about Threads from SCJP Practice Exam" New topic
Author

I doubt with question about Threads from SCJP Practice Exam

Maxim Maraev
Greenhorn

Joined: Sep 19, 2013
Posts: 8
Hi.
Sorry for my English.

Look for Question from SCJP 6 Practise Exams Book (Practice Exam III, question 25)

25. Given:
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.

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.
- 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.


Why it is impossible? id is not volatile.
So, I think it is possible to:
1) Thread 1 entering run() and take it`s own copy of id = 0
2) This time Thread 2 entering run and take it`s own copy of id = 0 -> both Threads
simultaneously change their own copy of id to 0
3) After that, both thread consequentially goes to synchronized pick()

Is it possible?

Thanks.

Max

OCPJP6
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1179
Francisco J. Bermejo wondered in Doubt regarding Threads found in K&B OCP Java 6 Practice Exams Book about option D as well with similar reasoning -

My reasoning is as follows: the first thread can execute "id = 1 - id;" so that id == 0 now, AND BEFORE it executes the next line (if (id == 0) { pick(); }) the second thread could execute "id = 1 - id;" so id would be id == 1 again (remember almost nothing in Java threads is guaranteed, and so is the order of execution of threads). Therefore BOTH threads would execute the method pick() (of course not at the same time, because it is synchronized in the class Stone's lock).


Regards,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61196
    
  66

"Max Mmmmmmm", please check your purple mooseages for an important administrative matter. Thanks.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Maxim Maraev
Greenhorn

Joined: Sep 19, 2013
Posts: 8
Dan Drillich wrote:Francisco J. Bermejo wondered in Doubt regarding Threads found in K&B OCP Java 6 Practice Exams Book about option D as well with similar reasoning -


Thanks.
So - it is very slippery topic and I think my assumption is right.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I doubt with question about Threads from SCJP Practice Exam