• 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

about Thread

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class S implements Runnable{
public S() {
}
int x=0,y=0;
synchronized void addX(){x++;}
synchronized void addY(){y++;}
void addXY(){x++;y++;}
boolean check(){return (x>y) ? true :false;}
public void run(){
///////
System.out.println(check()) ;
}
public static void main(String args[]){
S run = new S();
Thread t1 = new Thread(run);
Thread t2 = new Thread(run);
t1.start() ;
t2.start() ;
}
}
If this method are called in which order the check will return true?
A.call addX() and addY() simultaneously for number of times in run()
B.call addY() and addX() simultaneously for number of times in run()
c.add addXY() for number of times in run()
which answer is correct?
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you compiled and ran this code, what output did you get?
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe check will be false and will be printed and then it will exit, as you never do anything with your x & y methods to change the value from 0 : 0 there default values.
-Matt
[ March 19, 2002: Message edited by: Matt Ghiold ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
deezh,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you for your cooperation.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO to avoid the returning of true it is necessary to synchronized the modifications to the variables properly:
* delete addX and addY
* synchronize check and addXY
* make the variables private (not needed here but a good practise)
We need the first step because even if we synchronize all the methods that access the variables, one thread could intervene between the call to addX and addY peeking at x being greater than y. Though this is not likely in this example. It could be possible with more threads running.
As the code is I would say that option B is the only way to always get false. That is incrementing y before x.
Any insight?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
deezh,

You have been warned at least three times to change your display name. Your account has now been inactivated. You will need to re-register, using an appropriate display name, before you can post again.
reply
    Bookmark Topic Watch Topic
  • New Topic