java集合的不可修改的包装器使它们线程安全吗? [英] Does the unmodifiable wrapper for java collections make them thread safe?

查看:201
本文介绍了java集合的不可修改的包装器使它们线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使一个ArrayList的ArrayList的线程安全。我也不能让客户端更改集合。将不可修改的包装器使它线程安全,或者我需要两个包装器上的集合?

I need to make an ArrayList of ArrayLists thread safe. I also cannot have the client making changes to the collection. Will the unmodifiable wrapper make it thread safe or do I need two wrappers on the collection?

推荐答案

包装器将只阻止对它所收集的集合的更改,而不是集合中的对象。如果你有一个ArrayList的ArrayList,全局列表以及它的每个元素列表需要单独包装,你可能还需要做一些内容的列表。最后,您必须确保原始列表对象不会更改,因为包装器只能阻止通过包装器引用而不是原始对象的更改。

It depends. The wrapper will only prevent changes to the collection it wraps, not to the objects in the collection. If you have an ArrayList of ArrayLists, the global List as well as each of its element Lists need to be wrapped separately, and you may also have to do something for the contents of those lists. Finally, you have to make sure that the original list objects are not changed, since the wrapper only prevents changes through the wrapper reference, not to the original object.

在这种情况下不需要同步包装。

You do NOT need the synchronized wrapper in this case.

这篇关于java集合的不可修改的包装器使它们线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆