jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronized methods 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 "Synchronized methods" Watch "Synchronized methods" New topic
Author

Synchronized methods

doon chatters
Greenhorn

Joined: Jul 19, 2005
Posts: 4
Need help understanding the following SCJP question :

public class SychTest{
private int x;
private int y;
public void setX(int i){ x=i;}
public void setY(int i){y=i;}
public Synchronized void setXY(int i){
setX(i);
setY(i);
}
public Synchronized boolean check(){
return x!=y;
}
}

Under which conditions will check() return true when called from a different class?
A.check() can never return true.
B.check() can return true when setXY is callled by multiple threads.
C.check() can return true when multiple threads call setX and setY separately.
D.check() can only return true if SychTest is changed allow x and y to be set separately.

What I have understood till now is that when ever 'x' and 'y' are equal check() will return 'FALSE' (because the condition checked is x!=y , not x==y). Since what ever value is passed to setXY(int i) , the same value will be assigned to x and y , and x!=y will thus always return 'FALSE'.

Another possibility is that, if we change the method setXY(int i) to accept two int parameters instead of one, and assign the different parameters to x x and y in the following manner. Now if while invoking the method setXY() two different numbers as passed like this, setXY(22,20); the condition x!=y will be rendered' 'TRUE':
setXY(int i, int j)
{
setX(i);
setY(j);
}

In the light of the above two possibilities, I feel that A and D should be the correct answers.

However majority of sites state that "C" is supposed to be the answer. Can someone be kind enough to write a code and explain it, demonstrating that "C" is the correct answer.
janki tangeda
Ranch Hand

Joined: Jun 07, 2005
Posts: 54
setXY(int i) method sets the variables to the same value i, whereas setX(int i) and setY(int i) that are not synchronized methods, can be called by different threads(each thread passing a different value into the method).In such case check() will return true.

Options A & D are not correct.check() cannot return true only if the setX and setY methods are removed from the class.
doon chatters
Greenhorn

Joined: Jul 19, 2005
Posts: 4
Originally posted by doon chatters:
Need help understanding the following SCJP question :

public class SychTest{
private int x;
private int y;
public void setX(int i){ x=i;}
public void setY(int i){y=i;}
public Synchronized void setXY(int i){
setX(i);
setY(i);
}
public Synchronized boolean check(){
return x!=y;
}
}

Under which conditions will check() return true when called from a different class?
A.check() can never return true.
B.check() can return true when setXY is callled by multiple threads.
C.check() can return true when multiple threads call setX and setY separately.
D.check() can only return true if SychTest is changed allow x and y to be set separately.

What I have understood till now is that when ever 'x' and 'y' are equal check() will return 'FALSE' (because the condition checked is x!=y , not x==y). Since what ever value is passed to setXY(int i) , the same value will be assigned to x and y , and x!=y will thus always return 'FALSE'.

Another possibility is that, if we change the method setXY(int i) to accept two int parameters instead of one, and assign the different parameters to x x and y in the following manner. Now if while invoking the method setXY() two different numbers as passed like this, setXY(22,20); the condition x!=y will be rendered' 'TRUE':
setXY(int i, int j)
{
setX(i);
setY(j);
}

In the light of the above two possibilities, I feel that A and D should be the correct answers.

However majority of sites state that "C" is supposed to be the answer. Can someone be kind enough to write a code and explain it, demonstrating that "C" is the correct answer.



Janaki thanks for the reply. But since I am new at this, would it be too much to ask if you could code the calling class and the calling statements.
Thanks
doon chatters
Greenhorn

Joined: Jul 19, 2005
Posts: 4
I still have some faith on the option "A" ie (check() can never return true). Consider the following code:

public class TryFinally
{


public static void main(String str[])
{
SychTest st=new SychTest();
st.setXY(50);

if(st.check() == true)
{
System.out.println("True");
}
else
{
System.out.println("False");
}
}

}

class SychTest
{
private int x;
private int y;

public void setX(int i)
{
x=i;
}
public void setY(int i)
{
y=i;
}
public synchronized void setXY(int i)
{
setX(i);
setY(i);
}
public synchronized boolean check()
{
return x!=y;
}
}

Output : False


This always returns "False" , no matter what number you put.
So, shouldnt "A" a correct option?


If "C" is the answer, I still have "not" figured out a code where different threads from another class will invoke setX() and setY() seperately and pass different values, the class also should be able to invoke check().
Can anyone HELP in writing the code???
doon chatters
Greenhorn

Joined: Jul 19, 2005
Posts: 4
In reference to the Original posting: Can anybody verify that the following code is appropriate to demonstrate that option "C" should be the answer. Or is there anyother way of proving that.

public class ThreadCall extends Thread
{
static SychTest st=new SychTest();

public static void main(String str[])
{
Thread th1=new Thread(new Runnable(){
public void run()
{
st.setX(30);
System.out.println("Thread 1");
}
});
th1.start();
Thread th2=new Thread(new Runnable(){
public void run()
{
st.setY(30);
System.out.println("Thread 2");
}
});
th2.start();
if(st.check() == true)
{
System.out.println("True");
}
else
{
System.out.println("False");
}
}

class SychTest
{
private int x;
private int y;

public void setX(int i)
{
x=i;
}
public void setY(int i)
{
y=i;
}
public synchronized void setXY(int i)
{
setX(i);
setY(i);
}
public synchronized boolean check()
{
return x!=y;
}
}
 
 
subject: Synchronized methods