raj
raj

Reputation: 23

ArrayList thread safety

ArrayList is not Thread safe.what happens to thread safety when arraylist is passed as a method parameter.Method Parameters are generally thread safe

Upvotes: 0

Views: 209

Answers (2)

alpian
alpian

Reputation: 4748

I think you're a little muddled up between the stack and the heap. The handle/reference to you ArrayList passed as a method parameter is on the stack and therefore Thread-safe since the stack owned by that Thread is the only one that can access that reference.

However, the actual ArrayList lives in the heap and therefore many threads can access it at the same time and therefore you need to protect with some form of synchronization or, depending on the API, you may be able to get a "Synchronized" version of the ArrayList itself.

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273244

You cannot 'add' thread-safety like that.

If a class is not thread-safe you need to access it inside lock statements or something similar.

Upvotes: 3

Related Questions