File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can an array b synchronized

 
Mona Gadkari
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mona Gadkari
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Vicken
now I've seen word EXCEPT,
sorry again and thanks for explanation :roll:
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic