1

So if a program does this:

    static ArrayList<X> a = null;
    static{
            for(;;){X x = new X(); a.add(x)}
          }

and the only operations called after the static initializing, on the shared list, are get() and x.t()

       X x = a.get(i); x.t();

and X doesn't have access to container & is thread safe, that should mean that using Arraylist like this, without synchronization is thread safe, correct?

8
  • If the program starts with the static block code provides in your example, it will throw a ExceptionInInitializerError. Commented Nov 5, 2012 at 0:41
  • what does t do? Or is t any arbitrary function we can think of? Commented Nov 5, 2012 at 0:44
  • change that null to a new Arraylist<>, qestion stands. Commented Nov 5, 2012 at 0:44
  • X is thread safe, so it doesn't matter what t does, its a public thread safe method inside X, operating entirely on x state. Commented Nov 5, 2012 at 0:45
  • 1
    I don't think that is true, what if X.t modifies the container? Commented Nov 5, 2012 at 0:46

4 Answers 4

6

If nothing is modifying the ArrayList than there is no reason that you should be worried about it's thread-safety.

That static block is thread-safe by default, because it's ran only once when the class is loaded (for initialization).

Sign up to request clarification or add additional context in comments.

3 Comments

Agree. We just dont know what t does
... but if you can't guarantee that nothing is modifying the ArrayList, then I would argue that you should be worried about its thread safety.
@DanielPryden: I agree with that. But it's not mentioned anywhere in the OP. My answer was based on "and the only operations called after the static initializing, on the shared list, are get() ...".
6

If the intention is for the list to not be modified once it is created, then enforce that: wrap the ArrayList in a Collections.unmodifiableList().

Better yet, if you have Guava available, use an ImmutableList.

Comments

1

Vector is the synchronized collection you want.

Just because the elements of the Collection are thread safe does not guarantee that the container itself is thread safe. Based on my understanding of concurrency.

If t modifies the backing Collection it is not thread safe. For instance t is defined as follows:

this.list.remove(this.x);

It would not be safe.

6 Comments

No, Vector is not the answer. Please read here.
@LuiggiMendoza if that was the reason for a down vote that is suspect at best. A Vector is a perfectly legitimate collection to use in terms of a synchronization problem.
Down vote was mine. You should always prefer Collections.synchronizedList(new ArrayList<T>())) over Vector in modern Java because it makes the intent clearer. Better yet, use a lock-free implementation like CopyOnWriteArrayList.
In case you don't believe that Vector is not an option: Why is Java Vector class considered obsolete or deprecated?.
@LuiggiMendoza Oracle : docs.oracle.com/javase/7/docs/api/java/util/Vector.html does not have it deprecated so your argument is invalid on this one.
|
1

Thread-safety is only an issue on writable objects. Once initialised, your ArrayList is immutable - thus thread safe. Consider using unmodifiable wrappers around your arraylist - it will

  1. make clear the array is not meant to be modified
  2. prevent accidental modifications in the future

Just for sake of completeness, please be aware of the fact that, while operations on the ArrayList are threadsafe, operation on the elements are not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.