aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why autoboxing doesnt work here??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why autoboxing doesnt work here???" Watch "Why autoboxing doesnt work here???" New topic
Author

Why autoboxing doesnt work here???

MadhanKumar G R
Greenhorn

Joined: Jan 09, 2007
Posts: 1
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

Joined: Nov 01, 2006
Posts: 381
The syncronized block MUST contains a reference variable.
See syncronized block.


~Sanjeev Singh<br />SCJP 1.5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
anson chau
Greenhorn

Joined: Jan 01, 2007
Posts: 10
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

Joined: Jan 01, 2007
Posts: 10
yeah... just checked with a program..
nullPointerException , not to mention whether it is workable or not
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why autoboxing doesnt work here???