Help coderanch get a
new server
by contributing to the fundraiser
  • 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread topic Help needed

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was going through the mock exam of ETS Thread question (i have modifed the code a little bit .) :
public class Test{

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 return true when setXY is callled by multiple threads.

b.check() can return true when multiple threads call setX and setY separately.

I think B is right choice
please guide if i am wrong in my concept as setX and setY are public and can be called directly from differnet classes.
but making them private will never cause check() to return true.

And if i modify the code(below is modified code ) by making setX and setY private and synchronized and setXY() method non synchronized ,in this case i think calling setXY() by different threads cause the check() method return true.
Please correct my concept
private int x;

private int y;

private synchronized void setX(int i){ x=i;} //modified

private synchronized void setY(int i){y=i;} //modified

public void setXY(int i){ //modified

setX(i);

setY(i);

}

public Synchronized boolean check(){

return x!=y;

}

}

Please clarify my concept .
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First is incorrect because. See below program

class Test{
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;
}
}

public class another implements Runnable
{
public void run()
{
Test ob=new Test();
System.out.println(ob.check()+ " "+Thread.currentThread().getName());
}

public static void main(String ar[])
{
another Obj=new another();
Thread t1=new Thread(Obj,"one");
Thread t2=new Thread(Obj,"two");
Thread t3=new Thread(Obj,"three");
t1.start();
t2.start();
t3.start();
}
};

Second is correct because

class Test{
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;
}
}

public class another implements Runnable
{
public void run()
{
Test ob=new Test();
ob.setX(1);
ob.setY(2);
System.out.println(ob.check()+ " "+Thread.currentThread().getName());
}

public static void main(String ar[])
{
another Obj=new another();
Thread t1=new Thread(Obj,"one");
Thread t2=new Thread(Obj,"two");
Thread t3=new Thread(Obj,"three");
t1.start();
t2.start();
t3.start();
}
};

Vilish
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic