aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Qn on Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Qn on Threads" Watch "Qn on Threads" New topic
Author

Qn on Threads

Chandrakanth
Ranch Hand

Joined: Aug 16, 2005
Posts: 60
Given:
1. public class SyncTest {
2. private int x;
3. private int y;
4. private synchronized void setX( int i ) { x = i; }
5. private synchronized void setY( int i ) { y = i; }
6. public void setXY( int i ) { setX(i); setY(i); }
7. public synchronized boolean check() { return x != y; }
8. }
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.
What is the Answer?
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Chandrakanth Dn:
Given:
1. public class SyncTest {
2. private int x;
3. private int y;
4. private synchronized void setX( int i ) { x = i; }
5. private synchronized void setY( int i ) { y = i; }
6. public void setXY( int i ) { setX(i); setY(i); }
7. public synchronized boolean check() { return x != y; }
8. }
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.
What is the Answer?


I think the answer is B.

In case of thread nothing is impossible...

Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Hi Rathi,
can you please explain, how the answer is B?
Thanx in advance
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
1. public class SyncTest {
2. private int x;
3. private int y;
4. private synchronized void setX( int i ) { x = i; }
5. private synchronized void setY( int i ) { y = i; }
6. public void setXY( int i ) { setX(i); setY(i); }
7. public synchronized boolean check() { return x != y; }
8. }
Under which condition will check return true when called from a different class?
A. check can never return true.

Obviously not true as the value returned depends upon the individual values of x and y at the time of the method call.

B. check can return true when setXY is called by multiple threads.

True. Any number of threads can call setXY but there is no guarantee that the same thread will call setX and setY from within setXY, even though both these are synchronized.

C. check can return true when multiple threads call setX and setY separately.

True. But the multiple threads calling setX and setY don't necessarily have to supply the same values.

D. check can return true only if SyncTest is changed to allow x and y to be set separately.

This functionality is already available thru stX and setY. False
The question is "when can" so it's a little tricky. The roll of the dice could go either hence B and C are true.
HTH,
Sashi
[ December 28, 2005: Message edited by: Sasikanth Malladi ]
gutta rahul
Greenhorn

Joined: Nov 23, 2003
Posts: 12
public class ThreadTest extends Thread {
private SyncTest st = null;

public ThreadTest(SyncTest st) {
this.st = st;
}

public void set(int i) {
st.setXY(i);
System.out.println(st.check() + " " + getName());
}

public void run() {
for (int i = 0; i < 10; i++) {
set(i);
}
}

/**
* @param args
*/
public static void main(String[] args) {
SyncTest st = new SyncTest();
ThreadTest tt = new ThreadTest(st);
ThreadTest tt1 = new ThreadTest(st);
ThreadTest tt2 = new ThreadTest(st);
tt.start();
tt1.start();
tt2.start();
}
}

I tried writting some code using the same object with different Threads of execution and got only "False" as the output. If Iam wrong can someone come up with a better reasoning.
Rajat Gupta
Greenhorn

Joined: Sep 16, 2005
Posts: 7
quote riginally posted by Chandrakanth Dn:
---------------------------------------------------------------------------
Given:
1. public class SyncTest {
2. private int x;
3. private int y;
4. private synchronized void setX( int i ) { x = i; }
5. private synchronized void setY( int i ) { y = i; }
6. public void setXY( int i ) { setX(i); setY(i); }
7. public synchronized boolean check() { return x != y; }
8. }
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.
What is the Answer?
-------------------------------------------------------------------------

Hi,

I understand that the answer will be (C). Please correct me if I am wrong.

SetX, SetY and check are all synchronized methods. A lock by 1 thread on any one of these methods will imply an implicit lock on the other 2 remaining synchronized methods.

For option (B) B. check can return true when setXY is called by multiple threads.

Any thread that comes to execute setXY will have to accquire a lock on SetX method and that will place an implicit lock on the SetY too. So, using SetXY, no 2 threads can simultaneouly access SetX and SetY. So, in effect, SetXY can be called only in sequence by the threads. And as SetXY sets the value of both x and y as same, so the correct will always return false for B.

For Option (C)
C. check can return true when multiple threads call setX and setY separately.

If 1 thread changes the value of just x, by calling setX and releases the lock after that, then any next thread can access SetY and change the value of Y to a different value. Hence (C) can return false for correct.

Please correct me if I am wrong. Hope this helps.

cheers,
Rajat Gupta
gutta rahul
Greenhorn

Joined: Nov 23, 2003
Posts: 12
I tried to introduce sleep in between the two calls got setX and setY in the setXY() method to slow the process and it returned true. The lock on the object will be released after the completion of setX() method and then there is always a possiblity of another thread calling the check() method on the same object by acquiring the lock and return true. So the answer is B.
gutta rahul
Greenhorn

Joined: Nov 23, 2003
Posts: 12
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, long sleep) {
setX(i);
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
setY(i);

}

public synchronized boolean check() {
return x != y;
}

}

public class ThreadTest extends Thread {
private SyncTest st = null;

private long sleep;

public ThreadTest(SyncTest st, long sleep) {
this.st = st;
this.sleep = sleep;
}

public void set(int i) {
st.setXY(i, sleep);
System.out.println(st.check() + " " + getName());
}

public void run() {
for (int i = 0; i < 10; i++) {
set(i);
}
}

/**
* @param args
*/
public static void main(String[] args) {
SyncTest st = new SyncTest();
ThreadTest tt = new ThreadTest(st, 1000);
ThreadTest tt1 = new ThreadTest(st, 500);
ThreadTest tt2 = new ThreadTest(st, 100);
tt.start();
tt1.start();
tt2.start();
}
}

This is what I ran and concluded for answer B
Rajat Gupta
Greenhorn

Joined: Sep 16, 2005
Posts: 7
In response to gutta Rahul..

It seems that your assumptions is perfect in relation to the code that you have written. According to your code, as you changed the SetXY method, the first thread will execute the setX method (at this time only this thread will have the ownership of all the synchronized methods). Once this thread is done with SetX, it leaves the lock and goes to sleep, as SetXY is not synchronized.

In between, the next thread comes in and again takes ownership of the SetX method, and goes to sleep. After setX method is over, this second thread too leaves the ownership of all the synchronized methods.

Threads have different sleep timings and this is the reason that they go and execute the check method at different times, and that result in true.

So, (B) is perfect for the code you have given, but if the SetXY is not changed, I will still stick to (C).

Cheers,
Rajat Gupta
Lakshmanan Arunachalam
Ranch Hand

Joined: Nov 02, 2005
Posts: 99

So, (B) is perfect for the code you have given, but if the SetXY is not changed, I will still stick to (C).


B is true, even without adding a sleep. This will happen if you run the above code in Time sliced OS(UNIX) with a lot of threads calling setXY and check method.
Also, since setX and SetY are private methods, can't be called any threads directly.


Regards<br />Lakshmanan<br />IBM-OOAD & UML, SCEA-I
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Qn on Threads