java.util.Vectorthread safe?

Hi, first a general question - how do I find out which java classes are thread safe?

I have a class that holds things in a Vector, and looks roughly like:

class Bla

{

private Vector storage =new Vector();

publicvoid addObject(Object n)

{

synchronized(storage)

{

storage.add(n)

}

}

publicvoid removeObject(Object m)

{

synchronized(storage)

{

storage.remove(m);

}

}

publicint doSomeCalculation()

{

Object temp;

int sum;

synchronized(storage)

{

for(int i = 0; i < storage size; i++){

temp = get next object from storage

sum += temp.number;

}

}

return sum;

}

}

The three methods will be used by different threads - some threads will add objects, some will remove, and some will call doSomeCalculation. Do I need to synchronize all accesses to storage vector to prevent data corruption, or are storage.add() and storage.remove() synchronized inside the Vector class?

If I do have to synchronize, did I do it right?

Thanks

-Ben

[2106 byte] By [dark_venika] at [2007-10-1 2:59:44]
# 1
> ...how do I find out which java classes are thread safe?The API documents tell you.The tutorial tells you how to use threads: http://java.sun.com/docs/books/tutorial/essential/threads/index.html
ChuckBinga at 2007-7-8 15:38:05 > top of Java-index,Administration Tools,Sun Connection...
# 2
Tthe Vector docs only talk about threading issues when discussing fail-fast itterators. They don't say anything about add(), remove() methods being thread safe. How do I find out whether they are or not?Thanks-Ben
dark_venika at 2007-7-8 15:38:05 > top of Java-index,Administration Tools,Sun Connection...
# 3
Did you miss this?"Unlike the new collection implementations, Vector is synchronized."
ChuckBinga at 2007-7-8 15:38:05 > top of Java-index,Administration Tools,Sun Connection...
# 4
Yes I did - thank you.
dark_venika at 2007-7-8 15:38:05 > top of Java-index,Administration Tools,Sun Connection...