ArrayList线程安全
ArrayList 不是线程安全的。当 arraylist 作为方法参数传递时,线程安全会发生什么情况。方法参数通常是线程安全的
ArrayList is not Thread safe.what happens to thread safety when arraylist is passed as a method parameter.Method Parameters are generally thread safe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能像那样“添加”线程安全。
如果一个类不是线程安全的,您需要在lock语句或类似的语句中访问它。
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.我认为你在堆栈和堆之间有点混乱。作为方法参数传递的 ArrayList 的句柄/引用位于堆栈上,因此是线程安全的,因为该线程拥有的堆栈是唯一可以访问该引用的堆栈。
然而,实际的 ArrayList 存在于堆中,因此许多线程可以同时访问它,因此您需要使用某种形式的同步进行保护,或者,根据 API,您可能可以获得“同步”版本的ArrayList 本身。
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.