views:

59

answers:

3

ArrayList in java is thread safe.and it is implemented using array.

So, is the access to arrays in java thread safe??does the access to arrays needs to be synchronized??

+5  A: 

No, ArrayList isn't thread-safe in Java.

From the docs:

Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally.

Are you thinking of Vector, which uses synchronization internally? (Each method is synchronized, basically.)

Jon Skeet
it should be synchronized with sort of read-write lock, because reads can be simultaneous. if it uses exclusive lock then i don't like `Vector` :)
Andrey
@Andrey: Well, that's exactly what it does do. `Vector` is completely synchronized. (Which doesn't actually make it properly thread-safe... just each indivisual operation.)
Jon Skeet
@ohhh yes ArrayList isn't thread-safe but what about multiple threads access to an array.is it synchronized internally or we have to synchronize access to it externally??
@user301708: It's not synchronized, but bear in mind that an array can't change size, which makes things *somewhat* safer. Unless you add a memory barrier of some form, there's no guarantee that each thread will see the most recent value written by other threads.
Jon Skeet
A: 

What would a thread-safe array look like? You can't add or remove elements of an array. All you can do is assign values to individual members.

Suppose your code had

  int x = a[2];
  a[3] = x;

Is that thread safe? (Hint: possibly not, depends on how consistent you want a[2] and a[3] to be).

In general: start by being conspicuously thread-safe. Put the synchronization in - it's not so expensive. Really think about the semantics you mean. Test it and get the deadlocks out - if you have such problems you may well not have thought about what your are trying to do clearly enough. Only if your performance testing really shows this to be your bottleneck start to get clever.

djna
thread safety can't be *possibly* not. it can be yes or not.
Andrey
@Andrey - OK so is that code thread safe? Let's assume that array assignment is atomic, and forget all the register caching issues. Imagine a synchronized around the each of my two lines of code. Now is that code thread safe? You don't know, you can't know, because you need more information. Does my code intend that a[2] and a[3] always be consistent? Or do I not care? Without that information it's possible it's safe, it's possible it's not. I'm making the point that the OQ may be asking the wrong question.
djna
A: 

Java memory models consider each array element as a separate variable. You may have thread-unsafe actions on such a variable just like any other variables.

The array itself is quite thread safe.

static Object[] a;

// thread 1
a = new Object[10];

// thread 2
read a.length // ok  
read a[0]     // ok
a[0] = something   // ok

What's the big deal? If you use ArrayList for example, such actions are not safe without proper sync

static ArrayList a;

// thread 1
a = new ArrayList( another_collection_with_10_null );

// thread 2
a.size();  // unsafe
a.get(0);  // unsafe
a.set(0, something); // unsafe

You may get incorrect results, or even unsensible results, or exceptions. You may complete screw up the list and make it permanently unusable.

irreputable