This week's book giveaway is in the Agile/Processes forum.
We're giving away four copies of Building Green Software: A Sustainable Approach to Software Development and Operations and have Anne Currie, Sarah Hsu , Sara Bergman on-line!
See this thread for details.
  • 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:

Synchronized methods

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

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic