在多个类上共享线程安全集

发布于 2024-10-31 02:16:15 字数 504 浏览 1 评论 0原文

这个问题与我之前的一个问题有关。

我想要一个线程安全的集合,我可以在各种类中访问和修改它,有些是 Runnable,有些不是。

根据 JavaDoc 我得到了一个线程安全集,

  Set s = Collections.synchronizedSet(new HashSet());

但现在我想在我的程序中共享这个集。是否可以将其放入应用程序上下文中并在 XML 中对其进行初始化?

This question relates to one of my previous ones.

I'd like to have a thread-safe set which I can access and modify in various classes, some Runnables and some not.

According to the JavaDoc I'm getting a thread-safe set with

  Set s = Collections.synchronizedSet(new HashSet());

But now I want to share this set througout my program. Is it possible to put it in the application context and initialize it in an XML?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怕倦 2024-11-07 02:16:15

如果您认为它足以传递到您的 Runnable 中,那么我认为它没有理由不适合您的应用程序上下文。

我应该注意到,作为一般规则,全局变量为非常难以追踪错误打开了大门,您应该强烈考虑使用可以在运行时修改的单个应用程序范围上下文的替代方案。 (如果它只被读取,从未被写入,那么根本没有问题,我会考虑使用 Collections.unmodifyingSet( generatedHashset); 代替。

If you have deemed it to be adequate for passing into your Runnable's, then I see no reason why it wouldn't be suitable for your application context as well.

I should note that as a general rule, global variables open the door to very difficult to track down bugs, and you should strongly consider alternatives to having a single, application-wide context that can be modified at run-time. (If it's only ever read, never written, you don't have a problem at all, and I'd consider using Collections.unmodifiableSet(generatedHashset); instead.

做个少女永远怀春 2024-11-07 02:16:15

如果需要从多个线程访问和修改集合,则需要线程安全的集合。

在大多数情况下,ConcurrentHashMap 是我的首选。从 Java 6 开始,使用 Collections.newSetFromMap() 从映射创建集合是同步的。

Set<Type> concurrentSet = Collections.newSetFromMap(new ConcurrentHashMap<Type,Boolean>());

If you need to access and modify the set from multiple threads, you need a thread-safe set.

In most cases, ConcurrentHashMap is my preferred choice. Starting from Java 6, it's a synch to create a set out of a map by using Collections.newSetFromMap().

Set<Type> concurrentSet = Collections.newSetFromMap(new ConcurrentHashMap<Type,Boolean>());
ㄟ。诗瑗 2024-11-07 02:16:15

正如glowcoder提到的,将这个集合放入应用程序上下文会引发数据一致性问题。如果来自多个线程的任何更新访问 applicationContext 来修改 set ,则为 CopyOnWriteArrayList 或 CopyOnWriteArraySet。他们在更新时创建新的列表/集以避免并发修改异常。

CopyOnWriteArraySet< /强> .

As glowcoder mentioned putting this set to application Context invites data consistency problem. if any updates from multiple threads access applicationContext to modify set , then the CopyOnWriteArrayList or CopyOnWriteArraySet. They create a new list/set on updates to avoid concurrent modification exception.

CopyOnWriteArraySet .

同尘 2024-11-07 02:16:15

同步集并不总是足以保证线程安全。它只是保护单个方法调用。

如果您需要迭代该集合,则必须添加额外的同步或处理 ConcurrentModificationExceptions。

有很多类使用全局集,会增加死锁的可能性。

A synchronized set does is not always enough for thread safety. It just protects individual method calls.

If you need to iterate over the set, then you'll have to add extra synchronization or deal with ConcurrentModificationExceptions.

Having lots of classes using a global set, will increase the chances of dead locks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文