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

