Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why autoboxing doesnt work here???

 
MadhanKumar G R
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is an extract from SCJP1.5 K&B.


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(){return a+b;}
public void set(int a, int b){this.a=a;this.b=b;}

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

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

D. public int read(){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(){return a+b;}
public synchronized(this) void set(int a, int b){this.a=a;this.b=b;}

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



C and D are incorrect; only objects can be used to synchronize on.



What happened to autoboxing why doesnt it consider it as an Integer Object here?
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The syncronized block MUST contains a reference variable.
See syncronized block.
 
Henry Wong
author
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy "MadhanKumar G R",

Welcome to the JavaRanch...

You may not have noticed that we have a policy on screen names here at the ranch. It must consist of a first name, a space, and a last name. It must also not be fictitious.

Unfortunately, your screen name does not seem to conform with this policy. Please take a moment to change it.


Now as for your question...

What happened to autoboxing why doesnt it consider it as an Integer Object here?


The short answer is "autoboxing does not apply here". But if you want a longer answer, it would be really silly if it did.

Synchronization is really dependent on the object that you are using -- if the threads share the same object, then the threads will block each other, if they are not, then they are independent of each other. If autoboxing applied here, then since it is implementation dependent (ie. cache) on whether the objects are the same or not, it will be implementation dependent on how synchronization behaved.

Henry
[ January 10, 2007: Message edited by: Henry Wong ]
 
anson chau
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does synchronization works on null object ?
say :
// assume in a multi-threaded context
Integer a = null;
synchronized (a)
{
// code
}

I remembered I'd encountered this question but I forgot the answer and where to get it back ...

I'd be glad if somebody can give a quick answer
Thank you in advance
 
anson chau
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah... just checked with a program..
nullPointerException , not to mention whether it is workable or not
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic