Threadsafe和generic arraylist? [英] Threadsafe and generic arraylist?

查看:103
本文介绍了Threadsafe和generic arraylist?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个通用线程安全收集,我看到Arraylist可以很容易地通过其静态同步方法使用线程安全,但是我的错误是,这个ArrayList不是通用的,所以当我想使用我的对象我总是不得不施放他们。有更简单的方法吗?

I want to have a generic thread safe collection and I saw that the Arraylist can easily be used thread safe by its static Synchronized method but what bugs me is that this ArrayList is not generic so when I want to use my objects I always have to cast them. Is there an easier way to do this? Also other list types would be possible.

推荐答案

一点知识是一件危险的事情;-)是的,你可以使用Meta-骑士的建议,并使用 SyncRoot ,但你需要小心 - 这不是万灵药。请参阅来自Microsoft的BCL小组博客的此帖看看为什么它不像 SyncRoot 那么简单。来自该帖子的引用:

A little knowledge is a dangerous thing ;-) Yes, you could use Meta-Knight's suggestion and use SyncRoot, but you need to be careful - it's not a panacea. See this post from the BCL Team Blog from Microsoft to see why it's not as simple as SyncRoot. Quotes from that post:


我们的通用
集合的几个精明的用户观察到,我们不再
提供

A few astute users of our generic collections observed that we no longer provide a public SyncRoot property on ICollection<T>

SyncRoot

and


我认为我们最好强制
大致关于
同步,坦率地说,我们的通用
集合中的
SyncRoot 属性不提供任何值。

当然,这不是说它不能在任何情况下使用 - 你只需仔细考虑用例,而不是在汽车-pilot。

Of course, that's not to say it can't be used under any circumstances - you just have to think the use cases through carefully and not be on auto-pilot.

这篇关于Threadsafe和generic arraylist?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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