wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes chapter 9 self test Q.15 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 "chapter 9 self test Q.15" Watch "chapter 9 self test Q.15" New topic
Author

chapter 9 self test Q.15

pooja sharma
Greenhorn

Joined: Oct 24, 2007
Posts: 14
public class ThreadDemo {
synchronized void a() { actBusy(); }
static synchronized void b() { actBusy(); }
static void actBusy() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
public static void main(String[] args) {
final ThreadDemo x = new ThreadDemo();
final ThreadDemo y = new ThreadDemo();
Runnable runnable = new Runnable() {
public void run() {
int option = (int) (Math.random() * 4);
switch (option) {
case 0: x.a(); break;
case 1: x.b(); break;
case 2: y.a(); break;
case 3: y.b(); break;
}
}
};
Thread thread1 = new Thread(runnable);
Thread thread2 = new Thread(runnable);
thread1.start();
thread2.start();
}
}
Which of the following pairs of method invocations could NEVER be executing at the same
time? (Choose all that apply.)
A. x.a() in thread1, and x.a() in thread2
B. x.a() in thread1, and x.b() in thread2
C. x.a() in thread1, and y.a() in thread2
D. x.a() in thread1, and y.b() in thread2
E. x.b() in thread1, and x.a() in thread2
F. x.b() in thread1, and x.b() in thread2
G. x.b() in thread1, and y.a() in thread2
H. x.b() in thread1, and y.b() in thread2

what should be the output???
please help
vasu chowdary
Ranch Hand

Joined: Mar 15, 2008
Posts: 89
i think the answer should be A,F,H
is it correct?
pooja sharma
Greenhorn

Joined: Oct 24, 2007
Posts: 14
In the book the answer is given that A is right but in the pdf file provided in the cd of book it is given that A is incorrect.


so I got confused what is the answer.

According to the them answer is A,F,H
correct answer : A , F and H. A is incorrect because synchronized instance methods called on the same
instance, block each other. F and H could not happen because synchronized static
methods in the same class block each other, regardless of which instance was used to call
the methods. (An instance is not required to call static methods; only the class.)
wrong answer : C could happen because synchronized instance methods called on different instances
do not block each other. B, D, E, and G could all happen because instance methods and
static methods lock on different objects, and do not block each other.

please explain this program.what is the output and how it is coming.
manny singh
Greenhorn

Joined: Jan 18, 2008
Posts: 28
in f and h both accessing b() which have class level lock(static).so calling object doesn't matter it is locked for the first one and second doesn't access.
easy
 
 
subject: chapter 9 self test Q.15
 
Similar Threads
Doubt with thread
help me with Thread Problem
Thread's tangle
concurrent acess
Thread