This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt with thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt with thread" Watch "Doubt with thread" New topic
Author

Doubt with thread

Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
source http://www.valoxo.ch/jr/mocks/mock01a.html Q52

What is the output of the following code when compiled and run? Select two correct answers.
class A {
static int i=0;
public synchronized void aMethod(){ i++; }
public void bMethod(){
synchronized(this){ i--; }
}
}
public class Question52 implements Runnable {
A a = new A();
public static void main(String[] args) {
Thread t1 = new Thread(new Question52());
Thread t2 = new Thread(new Question52());
t1.start(); t2.start();
}
public void run(){
for(int i=0;i<10000000;i++){
a.aMethod(); a.bMethod();
}
System.out.print("a.i="+a.i+" ");
}
}

A. The output will always be a.i=0 a.i=0

B. The output cannot be determined.

C. The output will always be a.i=0 a.i=-1

D. t1 and t2 access the same member variable a.i.

E. Compilation error.

Ans:B,D
Doubt: how ans is B, how to go about s tricking such type of question?

pls help,
Gitesh
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
Hi Gitesh ,
Answer is correct. just do it carefully.
E is fine as program is compiling.
B ->
Static block runs when class is loaded,the very first thing.
Instance initializer runs when trying new'ng the class,before constructor runs.then constructor runs
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
oh i am sorry ...i replied in wrong post.
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Hi Gitesh,
In thread there is no predictable output.When a.aMethod() run & when a.bMethod(); run you dont know.


SCJP-1.5<br />SCWCD-1.4
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
dolly,
both the methods aMethod() and bMethod()use the same A reference a and as methods are synchronized only 1 thread will acquire lock....as both the methods are in run()..

Well in short i'm totally confused

SOS!! HELP!!
GiTeSh
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
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();
} }
Self Test Answers 753
754 Chapter 9: Threads
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
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.)
�˚ 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 read this care fully, this example is from the K & B book, chapter-9, example-15. I think it will help you.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Gitesh,

Regarding the first question posted, we have two runnable instances created and each have their own copy of Class A. Even though thread is calling the synchronized method of class A and thread acquires a lock on the Object, it would not block the other thread to continue with its execution and in fact very well can call the synchronized method of class A but on a different object. Since both the threads execute concurently, there is no way we can determine its output. I'm really not sure, If this is clarifying your doubt, but this is what I think.
Amrit Kashyap
Ranch Hand

Joined: Apr 23, 2006
Posts: 44
Hello Gitesh

The thing is that here variable "i" is static i.e class variable. And for thread to access class varible , they need to acquire "Class Lock". And only one thread can have access to lock at one time, so here both thread cannot access class varibale "i" at same time.

And moreover if you read the java specification for threads, you will come to know that thread behaviour is dependent on underlying OS and other environments. So their behaviour is always unpredicatable.
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Hi!!

I think the explanation given by Devashish Roy
is great!!!
Katrin Perry
Ranch Hand

Joined: Mar 07, 2007
Posts: 61
I don't think in this case the Class lock would be acuired. Method methodA is declared synchronized, therefore synchronized on this(object) and methodB's body is syncrhonized on (this) object. To be synchronized on the current object (this) is not the same as to be synchronized on the Class.
Which object/Class the synchronization is done upon, in the case of syncrhornized methods is decided depending on wheather the method is static or not, if it is static method, then synchronization is on the Class itself, if not it is on this (object). If just part of the method is synchronized, what comes after the keyword synchronized(...) is what we synchronize on. In the code above it would be 'this'.

Please let me know if you disagree, but this is my understanding of it.


Katrin
SCJP 5.0, SCWCD, SCBCD, Oracle Certified Master Enterprise Architect
 
 
subject: Doubt with thread
 
Similar Threads
even on synchronizing, why o/p cannot be determined??
simple doubt with operators
Thread
Question on thread topic
Doubt with constructor overloading