Pouton Gerald
Pouton Gerald

Reputation: 1625

Does casting a CopyOnWriteArrayList to a List cause it to lose its concurrency guaranties?

The above (title) is my main concern. And the case is

public class MyClass{
  CopyOnWriteArrayList<Stuff> min;
  ...
  public List<Stuff> get(){
    return min;
  }
}

Is the resulting List obtained from get() a liability? Does it affect what happens to min or is it its own copy? So for example, if I am iterating over the result of get() what's the effects?

Also while we are at it, how about the opposite? If I do

List<Stuff> stuffs = new ArrayList<Stuff>();
addABunchOfItems(stuffs);
CopyOnWriteArrayList safeStuff = (CopyOnWriteArrayList) stuffs;

What's the status of safeStuff and stuffs after the third line, concerning concurrency supports?

Upvotes: 4

Views: 3496

Answers (2)

fge
fge

Reputation: 121780

This:

CopyOnWriteArrayList safeStuff = (CopyOnWriteArrayList) stuffs;

will not work, it will raise a compile error; an ArrayList is not a CopyOnWriteArrayList.

As to "casting" a CopyOnWriteArrayList to a List, in fact you "don't", since CopyOnWriteArrayList implements the List interface.

Which means it respects the List contract; but internally, it is still a CopyOnWriteArrayList and no, concurrency guarantees are not lost. In fact you'll initiate one using:

final List<Whatever> list = new CopyOnWriteArrayList<>();

Note that while there are no dedicated contracts (interfaces) for concurrent implementations of List or Set, there is one for Maps: ConcurrentMap. Which does not mean you are forced to use, for instance:

final ConcurrentMap<Whatever> map = new ConcurrentHashMap<>();

if you do not need access to the specific methods of ConcurrentMap.

Upvotes: 5

Craig
Craig

Reputation: 1390

Casting an object to a different type doesn't change the type of the object. If the object being cast is unable to be cast, a ClassCastException will be raised at runtime. You need to instead create a new CopyOnWriteArrayList object like

List<Stuff> stuffs = new CopyOnWriteArrayList<Stuff>();

In the first example min will always be a able to be referenced like a CopyOnWriteArrayList<Stuff> returning it as a List<Stuff> doesn't change its type.

Upvotes: 3

Related Questions