• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Can you synchronize an array?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
David Mroczkowski
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael,
Thanks. Somehow I had it in my head that the code was written...
synchronized static int[] test = new int[5];
d
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone briefly explain what synchronized means, and how it affects an object? Thanks!
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Forget Steve. Look at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic