本文介绍了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:

SyncRoot

and

当然,这不是说它不能在任何情况下使用 - 你只需仔细考虑用例,而不是在汽车-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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:30
查看更多