• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I doubt with question about Threads from SCJP Practice Exam

 
Maxim Maraev
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Max Mmmmmmm", please check your purple mooseages for an important administrative matter. Thanks.
 
Maxim Maraev
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic