• 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 question

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assume you have a class that holds two private variables: a and b. Which of the following pairs can prevent concurrent access problems in that class? (Choose all that apply.)

A. public int read(int a, int b){return a+b;}
public void set(int a, int b){this.a=a;this.b=b;}

B. public synchronized int read(int a, int b){return a+b;}
public synchronized void set(int a, int b){this.a=a;this.b=b;}

C. public int read(int a, int b){synchronized(a){return a+b;}}
public void set(int a, int b){synchronized(a){this.a=a;this.b=b;}}

D. public int read(int a, int b){synchronized(a){return a+b;}}
public void set(int a, int b){synchronized(b){this.a=a;this.b=b;}}

E. public synchronized(this) int read(int a, int b){return a+b;}
public synchronized(this) void set(int a, int b){this.a=a;this.b=b;}

F. public int read(int a, int b){synchronized(this){return a+b;}}
public void set(int a, int b){synchronized(this){this.a=a;this.b=b;}}


Please explain why E is an incorrect answer. Thanks.
 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 2 forms of synchronized statements.

one is synchronized keyword which is used in methods to synchronize
the entire method

public synchronized int read(int a, int b){return a+b;}

two is synchronized block which is used to synchronize a block of
statements.

public int read(int a, int b){synchronized(this){return a+b;}}

For methods you can use only the keyword synchronized which doesn't
take any instance to synchronize on like
synchronized(this)


Hence E is incorrect
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi vishnu,
i cann't understand ur explaination i need some more explaination.can u plz explain it .why B is not correct i think the correct answer is B
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A. public int read(int a, int b){return a+b;}
public void set(int a, int b){this.a=a;this.b=b;}

Obviously incorrect. As there is no synchronization, and you have a race condition.

B. public synchronized int read(int a, int b){return a+b;}
public synchronized void set(int a, int b){this.a=a;this.b=b;}

Correct. The synchronization will solve the race condition.

C. public int read(int a, int b){synchronized(a){return a+b;}}
public void set(int a, int b){synchronized(a){this.a=a;this.b=b;}}

Incorrect. This one is a trick answer. As long as you synchronize on a common object, it should work. However, in this case, a is a primative, which can't be used for synchronization.

D. public int read(int a, int b){synchronized(a){return a+b;}}
public void set(int a, int b){synchronized(b){this.a=a;this.b=b;}}

Incorrect. Same reason as C. And even if it were allowed, it is not the same instance.

E. public synchronized(this) int read(int a, int b){return a+b;}
public synchronized(this) void set(int a, int b){this.a=a;this.b=b;}

Incorrect. Not legal Java syntax. This one will not compile.

F. public int read(int a, int b){synchronized(this){return a+b;}}
public void set(int a, int b){synchronized(this){this.a=a;this.b=b;}}

Correct. The synchronization on the common instance, this, should solve the race condition. In fact, this is the same instance as used in answer B.

Henry
 
Chitra AP
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all. Now I am clear.
 
A magnificient life is loaded with tough challenges. En garde tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic