包装器与并发收集之间的区别 [英] Difference between Wrappers and Concurrent collection

查看:35
本文介绍了包装器与并发收集之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以使用'collections.synchronizedCollection(Collection c)'来同步集合或"collections.synchronizedMap(Map c)",我们还可以使用Java并发API,例如ConcurrentHashMap或ArrayQueue或BlockingQueue.

we can synchronize a collection by using 'collections.synchronizedCollection(Collection c)' or 'collections.synchronizedMap(Map c)' and we can also use java concurrent API like ConcurrentHashMap or ArrayQueue or BlockingQueue.

这两种获取同步集合的方式之间的同步级别是否存在差异,或者几乎相同?

Is there any difference in synchronization level between these two way of getting synchronized collections or these are almost same?

有人可以解释吗?

推荐答案

是的:大规模并行处理期间的速度.

Yes: speed during massive parallel processing.

这可以用非常简单的方式说明:假设有100个线程正在等待从集合中取出某些东西.

This can be illustrated in a very simple way: Imagine that 100 Threads are waiting to take something out of a collection.

  • 同步方式:99个线程进入睡眠状态,而1个线程获得其值.

  • The synchronized way: 99 Threads a put to sleep and 1 Thread gets its value.

并发方式:100个线程立即获得其值,没有一个被搁置.

The concurrent way: 100 Threads get their value immediately, none is put on hold.

现在,第二种方法比简单的方法要花更多的时间,但是只要有2个线程(至少2个线程)不断使用它,那么由于 concurrent ,这是值得您节省的时间

Now the second method takes a little more time than a simple get, but as soon as a minimum like 2 Threads are using it on a constant basis, it is well worth the time you save thanks to concurrent execution.

这篇关于包装器与并发收集之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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