• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arraylist vs Vector

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
need to have some kind of clear understanding regarding the synchronized behavior of arraylist and vector. when we say that Vector is synchronized and arraylist is not, does it mean that
1. when multiple threads access vector and modify its contents, one thread will see updated copy of the vector before modification
2. when multiple threads access arraylist and modify its contents, one thread will see updated copy of the arraylist before modification only if the arraylist is properly synchronized.

is there anything more than what I said?

Regards,

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nitinram agarwal wrote:Hi,
need to have some kind of clear understanding regarding the synchronized behavior of arraylist and vector. when we say that Vector is synchronized and arraylist is not, does it mean that
1. when multiple threads access vector and modify its contents, one thread will see updated copy of the vector before modification
2. when multiple threads access arraylist and modify its contents, one thread will see updated copy of the arraylist before modification only if the arraylist is properly synchronized.

is there anything more than what I said?

Regards,



I found a related article. Might be explaining more than just Synchronization issue. If you have access to the source code for Vector and ArryaList implementation it will be lot helpful to see how the methods- which modify the list contents are implemented.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The methods on the Vector are synchronized meaning that only 1 thread can access it at a time. But a couple of successive method calls need external synchronization (e.g. checking if an element exists and if not inserting it).

So it's not who sees what but who can access the object.
 
nitinram agarwal
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so you mean even multiple threads cannot read the vector simultaneously. I was under the impression that its exlusive modification on the vector.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But it's also an exclusive read. Otherwise it's not properly synchronized.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic