包装器与并发收集之间的区别

编程入门 行业动态 更新时间:2024-10-18 20:22:43
本文介绍了包装器与并发收集之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们可以使用'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.

更多推荐

包装器与并发收集之间的区别

本文发布于:2023-11-29 16:20:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646928.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!