Question about synchronization with vectors

J

Jorge

Hi

I am a bit confused about the synchronization with vectors:
In my application I have a vector in which a few threads add and remove
objects and another thread reads it every once in a while.
By now it is working, but I want to avoid possible random errors when
the threads start to make more insertions and deletions in the vector.
I have read how to synchronize using the sychronize keyword around the
statements that use the vector, but I have also read that the vector is
thread-safe. So should I synchronize by hand the access to the vector
or the vector class handle it for me??

(I know it is a pretty newbie question, but I hope that this is the
place to understand this)

Thanks in advance!

Jorge
 
A

Alex Kizub

K

klynn47

If you look at the documentation for the Vector class, it will show you
which methods are synchronized.
 
V

VisionSet

Jorge said:
Hi

I am a bit confused about the synchronization with vectors:
In my application I have a vector in which a few threads add and remove
objects and another thread reads it every once in a while.
By now it is working, but I want to avoid possible random errors when
the threads start to make more insertions and deletions in the vector.
I have read how to synchronize using the sychronize keyword around the
statements that use the vector, but I have also read that the vector is
thread-safe. So should I synchronize by hand the access to the vector
or the vector class handle it for me??

(I know it is a pretty newbie question, but I hope that this is the
place to understand this)

The Vector class is synchronised, but all this means is that a **single**
method call (eg add(yourObject) is guaranteed atomic (that is thread-safe).
Only you can decide if you need further synced access.
If you do two operations on the collection that require nothing to have
changed inbetween then you will have to sync the calls:

synchronized(myVector) {
if(! myVector.contains(...)) myVector.add(...)
}

If you always guarantee thread safety externally like this, there is no need
to use a synced collection such as a Vector - which can basically be
considered deprecated anyhow. So use an ArrayList instead.
If you want a synced list, get one like this:

List myList = Collections.synchronizedList(new ArrayList());
 
M

Michael Borgwardt

Alex said:
You are right.



no, yes.

Wrong! If there are any sequences of operations that need atomicity,
especially an kind of iteration over the Vector's contents, then
the built-in synchronization is insufficient.
 
J

Jorge

Thank you to all!

I have decided to use a regular LinkedList and surround the iteration
loop and the add and remove calls with a synchronized(myList)...


Jorge
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top