Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Question about synchronization with vectors

Reply
Thread Tools

Question about synchronization with vectors

 
 
Jorge
Guest
Posts: n/a
 
      01-02-2005
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

 
Reply With Quote
 
 
 
 
Alex Kizub
Guest
Posts: n/a
 
      01-02-2005
> 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.


You are right.
>Unlike the new collection implementations, Vector is synchronized.

http://java.sun.com/j2se/1.4.2/docs/...il/Vector.html

> So should I synchronize by hand the access to the vector
> or the vector class handle it for me??


no, yes.
Alex Kizub.

 
Reply With Quote
 
 
 
 
klynn47@comcast.net
Guest
Posts: n/a
 
      01-02-2005
If you look at the documentation for the Vector class, it will show you
which methods are synchronized.

 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      01-02-2005


"Jorge" <> wrote in message
news: oups.com...
> 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());

--
Mike W


 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      01-03-2005
Alex Kizub wrote:
>>I have also read that the vector is
>>thread-safe.

>
>
> You are right.
>
>>Unlike the new collection implementations, Vector is synchronized.

>
> http://java.sun.com/j2se/1.4.2/docs/...il/Vector.html
>
>
>>So should I synchronize by hand the access to the vector
>>or the vector class handle it for me??

>
>
> 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.
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      01-04-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ primer statement about vectors containing vectors pauldepstein@att.net C++ 3 03-26-2008 06:22 PM
synchronization question gk Java 8 11-15-2006 01:43 PM
Ruby, SWIG and C++: how to properly wrap vector of vectors of doubles (2D vectors)? Ruby 0 09-14-2005 05:47 PM
Synchronization Question / Issue exits funnel Java 2 10-28-2003 11:46 AM
Synchronization question... matt melton Java 2 08-31-2003 01:50 PM



Advertisments