• 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 wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Qn on Threads

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rathi,
can you please explain, how the answer is B?
Thanx in advance
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic