开发者

ArrayList thread safety

开发者 https://www.devze.com 2023-02-28 23:24 出处:网络
ArrayList is not Thread safe.what happens to thread safety when arraylist is passed as开发者_高级运维 a method parameter.Method Parameters are generally thread safeYou cannot \'add\' thread-safety lik

ArrayList is not Thread safe.what happens to thread safety when arraylist is passed as开发者_高级运维 a method parameter.Method Parameters are generally thread safe


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消