• 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

Threads doubt from mock

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Abc{

private int x;

private int y;

public synchronized void setX (int i) (x=1

public synchronized void setY (int i) (y=1

public synchronized void setXY (int i) (setX(i) ; sety(i)

public synchronized Boolean check() { return x != y; }

}


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 only return true if Abc is changed to allow x and y to be set separately.


I think the check() method can never return true.

Please comment......................

Thank you in advance

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can iget to know the annswer.
I also think ,Check() never returns true
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMP 'c' is more likely to be the only option.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I feel choice D is correct and Abc can return false. Imagine a case when a thread has called setx() and sety() and setxy() are not called by any thread in that case value of x=1 and y=0( default intilization of member variables).
So if we remove the option of calling both the methods together then they will have diff values......

Please let us know the correct answer and the explantion also..........

Thanks,
Sarika.
 
Sarikaa Bhatnagar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

am sorry i think the sentence(Imagine a case when a thread has called setx() and sety() and setxy() are not called by any thread ) above is not very clear so reframing it.

Imagine a case when a thread has called setx() only and no other methods are called by any other thread(sety() and setxy() are not called by any thread ).

Thanks.
 
Srikanth Iyer
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is A)

but why not b)???

Please comment.............

Thank you in advacne
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please quote your sources.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic