This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on threads

 
Lavanya Raguram
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Amirr Rafique
Ranch Hand
Posts: 324
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For 2nd Question I think option A will also be correct. Someone please confirm
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For 2nd question , C and F are the right answer.
 
bnkiran kumar
Ranch Hand
Posts: 176
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A , B are same alternatives,

My answer is A,C,F

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.
 
Phani Kumar
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
bnkiran kumar
Ranch Hand
Posts: 176
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic