Java:某处是否还有另一种链表?

发布于 2024-10-30 02:33:46 字数 553 浏览 1 评论 0原文

是否有一个预先实现的链表可以使用 id 而不是数组索引来实现?

动机:我想在这里完成的是一个可以通过多个并行线程删除女巫单元的链表。 java的LinkedList的问题在于引用是索引引用。

澄清一下:假设我有三个线程和一个包含三个单元格的 LinkedList。线程 1 生成单元格 1(或 0,但无论如何...),...线程 3 生成单元格 3。所以基本上,线程向其单元格生成内容,这些单元格充当缓冲区。现在,如果线程 2 删除了单元格 2(出于业务逻辑原因),线程 #3 的引用将更改为 #2。当然,如果线程 #3 仍在单元 #3 上工作,则无法完成此操作,因此需要同步,即使线程 #3 当前未使用其单元,仍需要告知线程它将使用单元#2 从现在开始。

解决方案:如果使用 id 而不是数组索引引用来实现链表,则其他线程将永远不会看到更改,即可以安全地删除第二个单元格,因为线程 #3 将使用它的单元格,类似于 linkedlist.add (id 3, C cell) 而不是这样的 add(int index, C cell)。

我想类似的东西已经在某个地方实现了,但经过一段时间的谷歌搜索后,我找不到它。感谢所有帮助!

Is there a pre-implemented linked list out there that would have been implemented using id's instead of array indexes?

MOTIVATION: what I want to accomplish here is a linked list from witch cells can be removed by multiple threads in parellel. The problem with java's LinkedList is that the references are index ref's.

To clarify: let's say I have three threads and a LinkedList of three cells. Thread number one produces to cell number one (or 0 but anyway...), ...and thread number three produces to cell number 3. so basically threads produce stuff to their cells that work as buffers. Now if thread number 2 removes cell number two (for a business logical reason) the reference of thread #3 changes to #2. Naturally this can't be done if thread #3 is still working on cell #3, so synchronization is needed and even if thread #3 isn't currently using it's cell, the thread still needs to be told that it's going to use cell #2 from now on.

The solution: if the linked list would have been implemented using id's instead of array index references the other threads would never see the changes, i.e. the cell number two could be safely removed since thread #3 would use it's cell something like this linkedlist.add(id 3, C cell) instead of like this add(int index, C cell).

I guess something like this is already implemented somewhere but after a while of googling, I couldn't find it. ALL HELP APPRECIATED!

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

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

发布评论

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

评论(3

够运 2024-11-06 02:33:46

LinkedHashMap 似乎相关,但我不确定它是否完全适合您的场景。尝试一下。它是一个 HashMap,因此您可以使用项目的键来访问项目。

LinkedHashMap seems relevant, but I'm not sure if it completely fits your scenario. Try it. It's a HashMap so you are accessing items with their keys.

﹏半生如梦愿梦如真 2024-11-06 02:33:46

链接列表指的是“下一个”,当“N”是一个很大的数字时,尝试访问索引“N”处的项目时,使用链接列表中的索引可能会导致性能不佳。

为什么需要清单?订单有这么大的问题吗?为什么不能使用地图?

Linked Lists refer to the 'Next', working with Indices in Linked Lists can lead to bad performance when attempting to access the item at index 'N' when 'N' is a large number.

Why do you need a list? is the order that much of an issue? Why can't you use a Map?

梦罢 2024-11-06 02:33:46

按位置讨论传统 List 元素的方式只有两种。

  • 您可以通过索引引用列表元素;即从第一个元素开始到达该元素所需的步数。 (这就是 List.get(int) 的含义。)问题是元素的索引可能会由于列表上的其他操作而更改。

  • 您可以使用迭代器作为游标来引用列表元素。其他一些操作可能会删除您正在查看的元素之前或之后的元素,但这不会影响当前元素,前提是您使用适当的 List 实现。 (如果您使用非并发列表,则可能会遇到 ConcurrentModification 异常。)

底线是,如果您担心其他线程的更新导致索引更改,请不要使用索引。使用迭代器和支持并发修改的集合实现;例如,CopyOnWriteArrayListConcurrentLinkedQueue 或其他之一...具体取决于您想要执行的操作以及您需要的保证。


我不知道有任何 List 实现允许您锁定会更改元素位置的更新。 List API 不支持这种事情。

此外:

  • 锁定这样的列表可能是主要的并发瓶颈,并
  • LinkedList 上使用 get(int)set(int, Object)无法扩展。

There are only two ways of talking about a conventional List element by position.

  • You can refer to a list element by its index; i.e. by the number of steps it takes to reach the element starting from the first element. (That is what List.get(int) means.) The problem is that the index of an element can change due to other operations on the list.

  • You can refer to a list element using an Iterator as a cursor. Some other operation could remove an element before or after the one you are looking at, but this shouldn't effect the current one, provided that you use an appropriate List implementation. (If you use a non-concurrent list, you are likely to get ConcurrentModification exceptions.)

The bottom line is that if you are worried about some other thread's updates causing indexes to change, don't use indexes. Use an Iterator, and a collection implementation that supports concurrent modifications; e.g. CopyOnWriteArrayList, ConcurrentLinkedQueue or one of the others ... depending on exactly what you are trying to do and what guarantees you require.


I'm not aware of any List implementation that allows you to lock out updates that would change an element's position. The List APIs don't support this kind of thing.

Besides:

  • locking a list like that could be major concurrency bottleneck, and
  • using get(int) or set(int, Object) on a LinkedList doesn't scale.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文