java中处理并发数据插入的类

发布于 2025-01-01 19:27:30 字数 341 浏览 2 评论 0原文

始终会有 500 多个线程并发将唯一对象上传到存储桶。

在这种情况下,我应该使用哪种数据结构/类来实现java中的bucket。

仅供参考:

我尝试使用 ArrayList、Vector、ConcurrentHashMap、ArrayBlockingQueue、LinkedBlockingQueue。

ArrayList 失败,因为它不是线程安全的。 Vector 插入会消耗更多时间。 (因为获得监视器锁的等待时间很长)

...最后,我使用了 ArrayBlockingQueue,与其他队列相比听起来不错。

建议我是否存在适合这种情况的其他好的类/数据结构。

There will be 500+ threads concurrently uploading an unique object to a bucket all the time.

In this case, which data structure/class i should use to implement bucket in java.

FYI:

I tried using ArrayList, Vector, ConcurrentHashMap, ArrayBlockingQueue, LinkedBlockingQueue.

ArrayList fails as it is not thread safe.
Vector consumes more time for insertion. (as the wait time to get the monitor lock is high)

... finally, I used ArrayBlockingQueue which sounds good on comparing others.

Suggest me if any other good class/ data structure exists for this case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

↘人皮目录ツ 2025-01-08 19:27:30

争用将会非常高,因此您可能需要查看可用的无锁、无等待实现,或者 - 为了简单起见 - 使用 ConcurrentHashMap。

锁条带化是这里的关键优势,因此对于读操作,您没有锁,对于写操作,您仅锁定现有存储桶的子集,并且仅在重新散列时锁定整个哈希表。

您可以在这里找到更多信息:

性能 ConcurrentHashmap 与 HashMap

Java Hashtable 多次访问问题

Contention will be very high, so you may want to look at lock-free wait-free implementations available or - to keep it simple - use ConcurrentHashMap.

Lock striping is the key benefit here, so that for read operations you have no lock, for write operations you only lock a subset of existing buckets, and you lock the entire hashtable only for rehashing.

You can find a bit more here:

Performance ConcurrentHashmap vs HashMap

Java Hashtable many accesses problem

枯叶蝶 2025-01-08 19:27:30

您正在做出正确的决定来衡量绩效。

一般来说,LinkedBlockingQueue 在并发访问方面比 ArrayListBlockingQueue 表现更好,所以我肯定会尝试一下。

You are taking the right decision to measure the performance.

In general a LinkedBlockingQueue performs better for concurrent access than ArrayListBlockingQueue so I would definitely try that.

清风无影 2025-01-08 19:27:30

这实际上取决于您在添加内容之后或添加内容期间想要做什么。你首先需要考虑一下这一点。

在可以想象的最简单的情况下,也许您希望稍后以不特定的顺序迭代对象。如果是这样的话,就插入性能而言,ConcurrentLinkedQueue 可能是最好的。

It really depends on what you want to do after adding stuff, or during adding stuff. You first need to think about that.

In the most simple case imaginable, maybe you want to iterate the objects later in no particular order. If that's the case, ConcurrentLinkedQueue will probably be the best in terms of insertion performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文