GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  can an array b synchronized 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 " can an array b synchronized" Watch " can an array b synchronized" New topic
Author

can an array b synchronized

Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
Hello,
Arrays r objects in Java, so does this mean they too have locks - what i mean is - as we synchronize on an object can a array too be synchronized.
sample::
public class Test{
public static void main(String[] args){
synchronized(args){
// some code
}
}//main
-
-
}//class


Mona(Varijasmom)
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
hi Mona,
Any object is allowed in the synchronized block except
1) Primitives
2) Local variable that is an object reference
And yes array also have LOCKS.
Hope this helps.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
Originally posted by Vicken Karaoghlanian:
hi Mona,
Any object is allowed in the synchronized block except
1) Primitives
2) Local variable that is an object reference
And yes array also have LOCKS.
Hope this helps.

Hi Vicken
Thanks a lot.
Aleksandar Stojanovic
Greenhorn

Joined: Sep 08, 2003
Posts: 26
Hi Vicken
would you be kind to explain me why did you write
Any object is allowed in the synchronized block except
1) Primitives
public class Test {
protected int j = 10;
void changeValue(int _i){
synchronized ( j ) {
j = _i;
}
}
}

It doesn't work !

Am I misunderstood something?


Aleksandar Stojanovic
Aleksandar Stojanovic
Greenhorn

Joined: Sep 08, 2003
Posts: 26
Sorry Vicken
now I've seen word EXCEPT,
sorry again and thanks for explanation :roll:
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
You could not ask for a clearer example of a common cause of missing questions than the above messages. You MUST read the question carefully - it may ask "which of the following is illegal" or "which of the following is legal"
There is plenty of time, don't rush through the question text.
Bill
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Originally posted by Vicken Karaoghlanian:
hi Mona,
Any object is allowed in the synchronized block except
1) Primitives
2) Local variable that is an object reference
And yes array also have LOCKS.
Hope this helps.

Objects referenced by local variables are perfectly valid for synchronized blocks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can an array b synchronized