aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread question" Watch "Thread question" New topic
Author

Thread question

Chitra AP
Ranch Hand

Joined: May 25, 2005
Posts: 42
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

Joined: Nov 15, 2004
Posts: 1026
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


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
ashok kumar
Ranch Hand

Joined: Feb 28, 2005
Posts: 30
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Chitra AP
Ranch Hand

Joined: May 25, 2005
Posts: 42
Thank you all. Now I am clear.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread question