Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread question

 
Chitra AP
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • 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
 
ashok kumar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 21220
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you all. Now I am clear.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic