Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Question on threads

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For 2nd Question I think option A will also be correct. Someone please confirm
 
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For 2nd question , C and F are the right answer.
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic