Java - Thread safety of ArrayList constructors

Posted by andy boot on Stack Overflow See other posts from Stack Overflow or by andy boot
Published on 2010-04-21T09:49:21Z Indexed on 2010/04/21 9:53 UTC
Read the original article Hit count: 202

Filed under:
|
|

I am looking at this piece of code. This constructor delegates to the native method "System.arraycopy"

Is it Thread safe? And by that I mean can it ever throw a ConcurrentModificationException?

public Collection<Object> getConnections(Collection<Object> someCollection) {
    return new ArrayList<Object>(someCollection);
}

Does it make any difference if the collection being copied is ThreadSafe eg a CopyOnWriteArrayList?

public Collection<Object> getConnections(CopyOnWriteArrayList<Object> someCollection) {
    return new ArrayList<Object>(someCollection);
}

© Stack Overflow or respective owner

Related posts about java

Related posts about concurrency