Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on threads 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 "Question on threads" Watch "Question on threads" New topic
Author

Question on threads

Lavanya Raguram
Greenhorn

Joined: Apr 03, 2006
Posts: 27
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
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
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
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."
Sunil Kumar Gupta
Ranch Hand

Joined: Aug 26, 2005
Posts: 824
For 2nd question , C and F are the right answer.


Lack of will power has caused more failure than lack of intelligence or ability.
My Blog | Red5 Resources | Technology Update | Daily Technology Tips
bnkiran kumar
Ranch Hand

Joined: Mar 02, 2006
Posts: 176
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.


Kiran Kumar.
Phani Kumar
Greenhorn

Joined: Feb 15, 2002
Posts: 22
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
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
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: 176
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
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on threads
 
Similar Threads
Question on synchronisation
Thread question . Please help .
synchronize
Qn on Threads