R
Royan
I'm trying to find potential pitfall in unsynchronized methods that
return thread-safe collections. Assume i'm designing a thread-safe
class.
public class ThreadSafe {
private Vector<String> vector;
/** Normally i'd write something like */
public int someMethod() {
lock.lock();
try {
// Do thread-safe things and change vector
vector.clear()
} finally {
lock.unlock();
}
}
/** But is OK to have such method? */
public Vector<String> getVector() {
return vector;
}
}
Of course I would not bother about thread safety if there was just
Vector object, in my project there are plenty of other things that
really need to be synchronized, my only concern is #getVector() method
with respect to my question
return thread-safe collections. Assume i'm designing a thread-safe
class.
public class ThreadSafe {
private Vector<String> vector;
/** Normally i'd write something like */
public int someMethod() {
lock.lock();
try {
// Do thread-safe things and change vector
vector.clear()
} finally {
lock.unlock();
}
}
/** But is OK to have such method? */
public Vector<String> getVector() {
return vector;
}
}
Of course I would not bother about thread safety if there was just
Vector object, in my project there are plenty of other things that
really need to be synchronized, my only concern is #getVector() method
with respect to my question