wat will be the answer for this question? pls explain
public class SyncTest { private int x; private int y; private synchronized void setX( int i ) { x = i; } private synchronized void setY( int i ) { y = i; } public void setXY( int i ) { setX(i); setY(i); } public synchronized boolean check() { return x != y; } }
Under which condition will check return true when called from a different class? A. check can never return true. B. check can return true when setXY is called by multiple threads. C. check can return true when multiple threads call setX and setY separately. D. check can return true only if SyncTest is changed to allow x and y to be set separately.
Lavanya Raguram
Greenhorn
Joined: Apr 03, 2006
Posts: 27
posted
0
pls explain the answer for this question too
Thread Z holds the lock on object A. Thread X is blocked inside a wait call on ObjectA. What allows thread X to become runnable? A. Thread X is interrupted. B. Thread X is interrupted. C. Thread X�s wait() times out. D. Thread Z calls Thread.sleep(100); E. Thread Z releases the lock on A and calls the notify() method on thread X. F. Thread Z releases the lock on A and calls the notifyAll() method on objectA.
Amisha Shah
Ranch Hand
Joined: Mar 03, 2006
Posts: 33
posted
0
hi lavanya ,
i think ans to first question are A,D.
and ans to second Question are C,F.
but i m not sure about this answers. from where u get this Questions?
Amisha Shah.<br />SCJP 1.4
Amirr Rafique
Ranch Hand
Joined: Nov 14, 2005
Posts: 324
posted
0
For 2nd Question I think option A will also be correct. Someone please confirm
"Know where to find the solution and how to use it - that's the secret of success."
A is correct since if you call interrupt ,then it will make that thread active,(that is why you are catching Interrupted Exception)
C if you provided time as argument, then if that time is out then also it is eligible
F since notify is called on the object on which wait is called it will also (that is why wait,notify needs to be synchronized)
Hope it is clear.
Kiran Kumar.
Phani Kumar
Greenhorn
Joined: Feb 15, 2002
Posts: 22
posted
0
My answer to the second question E and F.
A is incorrect. Though thread X is interrupted, it will not become runnable until the object A's lock is freed. C is also incorrect. Even though the wait times out, thread X has to acquire the lock on object A before it can continue. D is incorrect. When an object holding a lock goes to sleep, it will not relinquish them. So there is no way for thread x to acquire lock on object A while thread Z is sleeping. E and F are correct as the only way thread X can become runnable is thread Z releasing the lock and notifying thread X about it. This is possible using notify() and notifyAll() only.
Abhijit Sontakey
Ranch Hand
Joined: Sep 26, 2005
Posts: 67
posted
0
Hi, My answer for the first question will be B check can return true when setXY is called by multiple threads. If multiple threads call the setXY() method, there is a probability that check() will return true if it is called after a thread has completed execution of the method. If the setX() and setY() methods are called independently, there is no way that the check() method will return true as X and Y can be se randomly by any thread.
Marx Villegas
Ranch Hand
Joined: Mar 10, 2006
Posts: 94
posted
0
Lavanya, regarding question 1, I think the answer is B. I did some modifications and some testing code, it is not too elegant, but it may help. Check this out:
import static java.lang.System.*;
class TestThread extends Thread { private TestClass t; private int milliseconds; private int setIto;
TestThread(TestClass t, int setIto, int milliseconds){ this.t = t; this.milliseconds = milliseconds; this.setIto = setIto; } public void run(){ t.setXY(setIto, milliseconds); } }
public class TestClass { private int x; private int y;
public static void main ( String[] argv) { TestClass tc = new TestClass(); TestThread tt1 = new TestThread(tc, 5, 50000); TestThread tt2 = new TestThread(tc, 6, 0);
tt1.start(); tt2.start();
try { tt2.join(); } catch (Exception ex){}
out.println(tc.check()); }
private synchronized void setX( int i ) { x = i; } private synchronized void setY( int i ) { y = i; } public void setXY(int i, int milliseconds) { setX(i); try { Thread.sleep(milliseconds); } catch (Exception unused) {} setY(i); } public synchronized boolean check() { return x != y; } }
Hope it helps Marx [ April 18, 2006: Message edited by: Marx Villegas ]
When the compiler's not happy, ain't nobody happy...
bnkiran kumar
Ranch Hand
Joined: Mar 02, 2006
Posts: 171
posted
0
Phani,
your answers are wrong as ,wait is called on an object so it is necessary to call notify or notifyall on that object but it is not necessary to release the lock on that object.
Phani Kumar
Greenhorn
Joined: Feb 15, 2002
Posts: 22
posted
0
No Kiran,
I agree with you that calling notify() or notifyAll() doesn't necessarily mean that the thread will give away the lock also. But in options E and F it was clearly mentioned that the thread Z first releases the lock on object A and then calls notify()/notifyAll() on thread X. I think I am correct in that respect. Your comments?