开发者

If all collection attributes are thread-safe , can we say that this collection is thread-safe?

If all attributes (or items fields, or data members) of a java collection are thread-safe (CopyOnWriteArraySet,ConcurrentHashMap, BlockingQueue, ...), can we say that this collection is thread-safe ?

an exemple :

pub开发者_如何学运维lic class AmIThreadSafe {

    private CopyOnWriteArraySet thradeSafeAttribute;

    public void add(Object o) {
        thradeSafeAttribute.add(o);
    }

    public void clear() {
        thradeSafeAttribute.clear();
    }
}

in this sample can we say that AmIThreadSafe is thread-safe ?


Assuming by "attributes" you mean "what the collection holds", then no. Just because the Collection holds thread-safe items does not mean that the Collection's implementation implements add(), clear(), remove(), etc., in a thread-safe manner.


Short answer: No.

Slightly longer answer: because add() and clear() are not in any way synchronized, and HashSet isn't itself synchronized, it's possible for multiple threads to be in them at the same time.

Edit following comment: Ah. Now the short answer is Yes, sorta. :)

The reason for the "sorta" (American slang meaning partially, btw) is that it's possible for two operations to be atomically safe, but to be unsafe when used in combination to make a compound operation.

In your given example, where only add() and clear() are supported, this can't happen.

But in a more complete class, where we would have more of the Collection interface, imagine a caller who needs to add an entry to the set iff the set has no more than 100 entries already.

This caller would want to write a method something like this:


void addIfNotOverLimit (AmIThreadSafe set, Object o, int limit) {
   if (set.size() < limit)      // ## thread-safe call 1
      set.add(o);               // ## thread-safe call 2
}

The problem is that while each call is itself threadsafe, two threads could be in addIfNotOverLimit (or for that matter, adding through another method altogether), and so threads A would call size() and get 99, and then call add(), but before that happens, it could be interrupted, and thread B could then add an entry, and now the set would be over its limit.

Moral? Compound operations make the definition of 'thread safe' more complex.


No, because the state of an object is the "sum" of all of its attributes.

for instance, you could have 2 thread-safe collections as attributes in your object. additionally, your object could depend on some sort of correlation between these 2 collections (e.g. if an object is in 1 collection, it is in the other collection, and vice versa). simply using 2 thread-safe collections will not ensure that that correlation is true at all points in time. you would need additional concurrency control in your object to ensure that this constraint holds across the 2 collections.

since most non-trivial objects have some type of correlation relationship across their attributes, using thread-safe collections as attributes is not sufficient to make an object thread-safe.


What is thread safety?

Thread safety simply means that the fields of an object or class always maintain a valid state, as observed by other objects and classes, even when used concurrently by multiple threads.

A thread-safe object is one that always maintains a valid state, as observed by other classes and objects, even in a multithreaded environment.

According to the API documentation, you have to use this function to ensure thread-safety:

synchronizedCollection(Collection c) 
         Returns a synchronized (thread-safe) collection 
         backed by the specified collection

Reading that, it is my opinion that you have to use the above function to ensure a thread-safe Collection. However, you do not have to use them for all Collections and there are faster Collections that are thread-safe such as ConcurrentHashMap. The underlying nature of CopyOnWriteArraySet ensures thread-safe operations.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜