File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Can you synchronize an array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can you synchronize an array?" Watch "Can you synchronize an array?" New topic
Author

Can you synchronize an array?

David Mroczkowski
Greenhorn

Joined: Nov 07, 2002
Posts: 12
I'm reading in Using Java 2 by Joseph L. Weber that "Array declarations are composed of the following parts: Array modifiers | Optional |The keywords public, protected, private, or synchronized."
I understand how an array can be public, protected or private; but synchronized.
The book was publish in 1998. Has this changed?
d
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi David,
Welcome to JavaRanch.
An array is an Object. So, just as you can synchronzie on any Object, you can sync an array.
For example:

That's pretty useless code, but it does demonstrate that arrays are instantiated with new like any Object and have valid wait() and notifyAll() methods.
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
David Mroczkowski
Greenhorn

Joined: Nov 07, 2002
Posts: 12
Michael,
Thanks. Somehow I had it in my head that the code was written...
synchronized static int[] test = new int[5];
d
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I would have thought the same thing David did, based on what he quoted. And no, you can't declare an array as synchronized. That's not something that's changed - you've never been able to declare an array aynchronized. The book is wrong. You can synchronize on an array (after its declaration and instantiation) as Michael showed - but you can't use synchronized as a modifier in the declaration.


"I'm not back." - Bill Harding, Twister
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
I guess I never considered that anyone would conceive of declaring a reference as synchronized. I just assumed that the intended meaning was that unlike a primitive, an array could be used as the reference of a synchronized block. Surely that was the author's intent though admittedly it was not phrased that way.
Michael Morris
Alex McCormick
Ranch Hand

Joined: Mar 08, 2003
Posts: 31
Can someone briefly explain what synchronized means, and how it affects an object? Thanks!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You should probably post your question as a new topic. However, in brief, synchronization deals with mult-threaded programs and controlling data access. You should first learn about threads before you delve into synchronization. You can find plenty of stuff about threads on the Web.I suggest you start with the tutorials on Sun's web site.
HTH
Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can you synchronize an array?
 
Similar Threads
Thread class
Static variable Question?
what are all the modifiers in java...
What is the difference between instance variables and non local variables
Reg.Interface declaration