• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arraylist vs Vector  RSS feed

 
nitinram agarwal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • 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,

 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • 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: 90
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
But it's also an exclusive read. Otherwise it's not properly synchronized.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!