Java 当我只使用提供和轮询访问 LinkedList 时,它是否是线程安全的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3362018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:35:26  来源:igfitidea点击:

Is LinkedList thread-safe when I'm accessing it with offer and poll exclusively?

javamultithreadinglistthread-safetylinked-list

提问by André Hoffmann

I have a linked list samples:

我有一个链表samples

protected LinkedList<RawDataset> samples = new LinkedList<RawDataset>();

I'm appending elements to the list in thread 1 like this:

我将元素附加到线程 1 中的列表中,如下所示:

this.samples.offer(data);

And I'm retrieving elements from it in a second thread like so:

我在第二个线程中从中检索元素,如下所示:

public RawDataset retrieveSample() {
    return this.samples.poll();
}

Would this be considered as thread-safe? Even though thread 1 and 2 are both modifying the list they only do so on either the head or the tail of the list exclusively, right?

这会被认为是线程安全的吗?即使线程 1 和 2 都在修改列表,但它们只在列表的头部或尾部专门修改,对吗?

If it isn't can anyone point me to a class in the Java API that comes with poll/offerand is sure to be thread-safe?

如果不是,任何人都可以指出我在 Java API 中带有poll/offer并且确定是线程安全的类吗?

Thank you in advance.

先感谢您。

BTW: Collections.synchronizedList(new LinkedList())won't give me access to offer/poll.

顺便说一句:Collections.synchronizedList(new LinkedList())不会让我访问offer/ poll

采纳答案by nos

LinkedList is not thread safe. You'd have to do the locking yourself.

LinkedList 不是线程安全的。您必须自己进行锁定。

Try ConcurrentLinkedQueueor LinkedBlockingDequeinstead if it fits your needs, they are thread safe but slightly different behavior than LinkedList.

如果满足您的需要,请尝试使用ConcurrentLinkedQueueLinkedBlockingDeque,它们是线程安全的,但与 LinkedList 的行为略有不同。

回答by nanda

No LinkedList is not thread safe. Use LinkedBlockingDequeinstead

没有 LinkedList 不是线程安全的。使用LinkedBlockingDeque代替

回答by Benoit Courtine

if you have a JDK, you can look at the source code of "Collections.synchronizedList()". It is simple, so you can create a copy of this method specialized to get both LinkedList and synchronization functionnalities.

如果你有JDK,可以看一下“Collections.synchronizedList()”的源码。这很简单,因此您可以创建此方法的副本,专门用于获取 LinkedList 和同步功能。

public class SynchronizedLinkedList<T> implements List<T> {

    private LinkedList<T> list;

    private Object lock;

    public void add(T object) {
        synchronized(lock) {
            list.add(object);
        }
    }

    // etc.
}

回答by justadev

That is correct - LinkedList is not synchronized and thus not thread safe. If you do not want to use the newer synchronized analogies of LinkedList, namely, ConcurrentLinkedQueue or LinkedBlockingQueue, you can initialize LinkedList like this:

这是正确的 - LinkedList 不是同步的,因此不是线程安全的。如果您不想使用 LinkedList 的更新的同步类比,即 ConcurrentLinkedQueue 或 LinkedBlockingQueue,您可以像这样初始化 LinkedList:

LinkedList<RawDataset> samples = (LinkedList)Collections.synchronizedList(new LinkedList<RawDataset>());