aspose file tools*
The moose likes Java in General and the fly likes Difference B/w ArrayList & Vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference B/w ArrayList & Vector" Watch "Difference B/w ArrayList & Vector" New topic
Author

Difference B/w ArrayList & Vector

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 417
I have read that ArrayList is not thread safe while Vector is thread safe.
Plz tell me how i can prove it programmatically.
If possible plz send me a code for this also.
Thanx.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Why prove this programatically when the documentation states exactly this?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[warning - impending rant alert]

Sun says that Vector is "thread safe". It's true that Vector is synchronized. It's fairly rare that it's actually safe to use it without additional external synchronization. The reason is that you almost always end up using two or more different method calls, and if another thread cuts in between the two calls, strange things happen. For example:

What happens if another thread calls add() or remove() between the size() and get() calls? You may process one list element twice, or skip one, or get a NoSuchElementException.

The person who invented the myth that synchronization automatically makes a class "thread-safe" needs to be keelhauled. That idea has led to a great deal of bad multithreaded code which is difficult to debug.

For what it's worth, Collections.synchronizedList() has the same problem. On the other hand, Hashtable and Collections.synchronizedMap() are comparatively safe, because most of the operations people need these for are fairly atomic. As long as you don't need to iterate through the entire map...
[ May 17, 2006: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Jim Yingst:
For what it's worth, Collections.synchronizedList() has the same problem. On the other hand, Hashtable and Collections.synchronizedMap() are comparatively safe, because most of the operations people need these for are fairly atomic. As long as you don't need to iterate through the entire map...


In which case in 1.5 we have some nice Collections in java.util.concurrent that will allow you to safely iterate even in the face of concurrency.
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 417
If external synchronization needs to be done for vector,then how is vector synchronized and why ArrayList is unsynchronized.
I am not clear.
There must be some strong reason to say :
Vector is synchronized and ArrayList in unsynchronized...
Plz clarify it.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

The individual methods of Vector are synchronized, and of ArrayList unsynchronized; that's all it means.


[Jess in Action][AskingGoodQuestions]
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 417
Method of a vector are synchronized???
I am not clear.
Plz send a code snippet for this.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18150
    
    8

Code snippet? Okay.This is how you declare a method to be synchronized.
[ May 18, 2006: Message edited by: Paul Clapham ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

Originally posted by Raj Kumar Bindal:
If external synchronization needs to be done for vector,then how is vector synchronized and why ArrayList is unsynchronized.
I am not clear.
There must be some strong reason to say :
Vector is synchronized and ArrayList in unsynchronized...
Plz clarify it.


Synchronization in the case of the vector (or any threadsafe collection) is used to protect the collection from being corrupted. It is not aware of the particulars of the data.

For example...



In this case, we are sure that "Hello" and "World" will definitely be added correctly, as the add() method is synchronized. But what if "World" must come after "Hello"? It is possible for another thread to add a value between "Hello" and "World" -- as there is a point between those two methods where the lock is released.

To assure atomicity between these two calls, we need to do this...



In this case, external synchronization needs to be done, even though the add() method of the vector class is already synchronized.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 417
Thanx Henry
you told very clearly
But just send me one example that proves vector is synchronized while arraylist is not synchronized.
**If this is the case,I have used many arraylist in my application,i will replace all of them by vector.
Plz clarify it.
Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 92
Incase you get any example which demonstrates your question, please share in this forum also. By the way, donot change all arraylists to vector, as synchronisation should be used only in places of requirement. It can prove to be overheadif used unnecessarily.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

Originally posted by Raj Kumar Bindal:
Thanx Henry
you told very clearly
But just send me one example that proves vector is synchronized while arraylist is not synchronized.
**If this is the case,I have used many arraylist in my application,i will replace all of them by vector.
Plz clarify it.


Not sure why you need proof for this -- the JavaDoc for ArrayList states that it is not synchronized. And the JavaDoc for Vector states that it is synchronized.

Furthermore, whether something is synchronized, doesn't make it better. Synchronization is not necessary if only one thread is accessing the collection at a time.


But if you want proof, you can always examine the source code that can be installed with the JDK.

Henry
[ May 18, 2006: Message edited by: Henry Wong ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Originally posted by Raj Kumar Bindal:
I have used many arraylist in my application,i will replace all of them by vector.


That would be silly -- ArrayList is intended as an improved replacement for Vector!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference B/w ArrayList & Vector
 
Similar Threads
what is the difference between the ArrayList and Vector?
StringTokenizer in Jdk1.5
Vector Vs ArrayList
What is the difference between ArrayList and Vector?
Two strange exception while working with collections concurrently.