• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Thread Question

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw this question in a mock exam. Can anyone explain how to arrive at the answer?

public class Test {
private int a;
private int b;
private synchronized void A(int c) {a=c;}
private synchronized void B(int c) {b=c;}
public void AB(int c) {A(c); B(c);}
public synchronized boolean d() {return a != b;}
}
Under which condition will d return true when called from a different class?
A. d can never return true when multiple threads call A and B separately
B. d can never return true only if Test is changed to allow a and b to be set separately
C. d can never return true when AB is called by multiple threads
D. d can never return true
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"constructicons"
Welcome to the JavaRanch! Please adjust your displayed name to meet the JavaRanch Naming policy.
You can change your displayed name here.
Thanks and again, welcome to the JavaRanch.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Weird question. It asks when d will return true, yet the options state when d never returns true.
A. d can never return true when multiple threads call A and B separately
It can still return true when the AB() and d() methods are running at the same time (?)

B. d can never return true only if Test is changed to allow a and b to be set separately
Huh? In fact, there's a greater likelihood of d returning true if Test is changed this way!

C. d can never return true when AB is called by multiple threads
D. d can never return true
refer to (A.)
 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic