Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

difference between ArrayList & Vector

  Asked By: Chisisi    Date: Apr 06    Category: Java    Views: 1445

We know that there is one major difference between ArrayList &
Vector that Vector is Synronized, what does that mean, I would like to ask
1. If multiple threads are accessing that Vector object say we are adding
element, we can write it down using Syncronized method or Syncronized block so
what does it mean that Vectors are internally syncronized we can achieve
Syncronization using the above stuff, please tell me the detail answer.



1 Answer Found

Answer #1    Answered By: Hattie Howard     Answered On: Apr 06

a "synchronized" piece of code is code that at any given point in time
can be executed by only one thread. Suppose for example that your
application has two threads  that sometimes try to insert a new record
into a vector  or an ArrayList. Now with the ArrayList it can happen
that thread T1 begins to change the ArrayList internally; then the
scheduler activates thread T2 which changes the ArrayList and appends
its new entry. Now the scheduler switches back to T1 which -using the
old values of its internal variables- will "update" the ArrayList and
in effect will overwrite the contents just defined by T2. Which means
that the data from T2 are lost.

With a vector this cannot happen. If T1 is inserting into a vector, no
other thread or process can change the vector in any way until T1 is
finished with inserting a new record and the state of the vector is
stable again.

Should you know a bit about relational databases: think of a Vector as
working with transactions; an ArrayList, on the other hand, can be
changed by several distinct threads at the same data, and the date can
become completely inconsistent.

Hope that clarifies this matter. If not, please tell us what you don't

Didn't find what you were looking for? Find more on difference between ArrayList & Vector Or get search suggestion and latest updates.