Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Chisisi Massri   on Apr 06 In Java Category.

  
Question Answered By: Hattie Howard   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
understand.

Share: 

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


Tagged: