使您的收藏线程安全的? [英] Make your collections thread-safe?

查看:106
本文介绍了使您的收藏线程安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设计一个集合类,没有任何理由不执行锁定私下使它线程安全的?或者我应该离开这个责任到收集的消费者?


解决方案

没有任何理由不执行锁定私下使它线程安全的?




这要看情况。你的目标写它是由多个线程访问的集合类?
如果是这样,使它线程安全的。如果没有,不要浪费你的时间。这种事情是什么人是指当他们谈论过早优化



解决您有问题。不要试图解决,你认为你可能会在未来几年未来的问题,因为你看不到未来,而且你总错了。



请注意:您仍然需要编写代码的维护的方法,例如,如果你的没有的需要一起去,并添加锁定到集合,它不会是非常困难的。我的观点是没有实现,你不需要的功能,也不会用


When designing a collection class, is there any reason not to implement locking privately to make it thread safe? Or should I leave that responsibility up to the consumer of the collection?

解决方案

is there any reason not to implement locking privately to make it thread safe?

It depends. Is your goal to write a collection class which is accessed by multiple threads? If so, make it thread safe. If not, don't waste your time. This kind of thing is what people refer to when they talk about 'premature optimization'

Solve the problems that you have. Don't try to solve future problems that you think you may have some years in the future, because you can't see the future, and you'll invariably be wrong.

Note: You still need to write your code in a maintainable way, such that if you did need to come along and add locking to the collection, it wouldn't be terribly hard. My point is "don't implement features that you don't need and won't use"

这篇关于使您的收藏线程安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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