gpt4 book ai didi

java - LinkedBlockingQueue 使用非线程安全列表

转载 作者:行者123 更新时间:2023-12-01 11:52:55 25 4
gpt4 key购买 nike

我想知道为什么不 LinkedBlockingQueue如果我们将底层数据结构更改为像 java.util.LinkedList 这样的非线程安全列表,是否可以工作?当我尝试时,我得到一个 NoSuchElementException 。它不是由锁(takeLock)保护的,这使得它是线程安全的吗?

private final List<E> list;
private final ReentrantLock takeLock;
private final ReentrantLock putLock;
private final Condition notFull;
private final Condition notEmpty;
private final AtomicInteger count;

public LinkedBlockingQueue() {
takeLock = new ReentrantLock();
putLock = new ReentrantLock();
notFull = putLock.newCondition();
notEmpty = takeLock.newCondition();
count = new AtomicInteger(0);
list = new LinkedList<E>();
}

public E get() {
E item = null;
int c = -1;
try {
takeLock.lockInterruptibly();
while (count.get() == 0) notEmpty.await();

// original -> item = dequeue();
item = list.remove(); // NoSuchElementException

c = count.getAndDecrement();
}
catch (InterruptedException ie) {}
finally {
takeLock.unlock();
}

if (c == capacity) signalNotFull();
return item;
}

public void put(E e) {
int c = -1;
try {
putLock.lockInterruptibly();
while (count.get() == BOUND_SIZE) notFull.await();

// original -> enqueue(node);
list.add(e);

c = count.getAndIncrement();
}
catch (InterruptedException ie) {}
finally {
putLock.unlock();
}

if (c == 0) signalNotEmpty();
}

最佳答案

您正在使用两个单独的锁定对象:

takeLock = new ReentrantLock();
putLock = new ReentrantLock();
notFull = putLock.newCondition();
notEmpty = takeLock.newCondition();

这是错误的。首先,您必须对 put 和 take 操作使用相同的锁对象。此外,您必须使用相同的锁定对象创建条件。

lock = new ReentrantLock();
notFull = lock.newCondition();
notEmpty = lock.newCondition();

并且您应该使用给定的锁引用替换 takeLock 和 putLock 用法。

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Lock.html http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Condition.html

关于java - LinkedBlockingQueue 使用非线程安全列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28655511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com