使用`LinkedBlockingQueue`可能导致空指针异常

caihuadaye :

我最近正在学习Java并发编程。我知道final关键字可以保证安全的发布。但是,当我阅读LinkedBlockingQueue源代码时,发现headand last字段未使用final关键字。我发现该enqueue方法方法中被调用put,并且该enqueue方法直接将值分配给last.next此时,last可能是null因为last未用声明final我的理解正确吗?虽然lock可以保证last读写线程的安全性,但是可以lock保证last不是一个正确的初始值null

public class LinkedBlockingQueue<E> extends AbstractQueue<E>
        implements BlockingQueue<E>, java.io.Serializable {
transient Node<E> head;
private transient Node<E> last;
public LinkedBlockingQueue(int capacity) {
        if (capacity <= 0) throw new IllegalArgumentException();
        this.capacity = capacity;
        last = head = new Node<E>(null);
    }
 private void enqueue(Node<E> node) {
        // assert putLock.isHeldByCurrentThread();
        // assert last.next == null;
        last = last.next = node;
    }

public void put(E e) throws InterruptedException {
        if (e == null) throw new NullPointerException();
        // Note: convention in all put/take/etc is to preset local var
        // holding count negative to indicate failure unless set.
        int c = -1;
        Node<E> node = new Node<E>(e);
        final ReentrantLock putLock = this.putLock;
        final AtomicInteger count = this.count;
        putLock.lockInterruptibly();
        try {
            /*
             * Note that count is used in wait guard even though it is
             * not protected by lock. This works because count can
             * only decrease at this point (all other puts are shut
             * out by lock), and we (or some other waiting put) are
             * signalled if it ever changes from capacity. Similarly
             * for all other uses of count in other wait guards.
             */
            while (count.get() == capacity) {
                notFull.await();
            }
            enqueue(node);
            c = count.getAndIncrement();
            if (c + 1 < capacity)
                notFull.signal();
        } finally {
            putLock.unlock();
        }
        if (c == 0)
            signalNotEmpty();
    }
}
伊万:

根据此博客文章https://shipilev.net/blog/2014/safe-public-construction/甚至final在构造函数中写入一个属性也足以实现安全的初始化(因此,您的对象将始终被安全地发布)。并且capacity属性声明为final

简而言之,在三种情况下,我们会遇到障碍:

最后一个领域是这样写的。注意,我们并不关心实际写入哪个字段,我们在退出(initializer)方法之前无条件地发出了屏障。这意味着,如果您至少有一个final字段写操作,则final字段语义将扩展到构造函数中编写的所有其他字段。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

由于空指针异常导致应用崩溃

Proguard收缩导致空指针异常

LibGDX:newServerSocket导致空指针异常

在拷贝构造函数导致空指针异常使用直接字段访问而非getter方法

尽管使用when和then,模拟存储库功能仍会导致空指针异常

使用科特林弹簧启动时启用猬导致空指针异常

切换到另一个片段后使用RunOnUIThread导致空指针异常

导航组件:从全球行动导致空指针异常传递参数

为什么String.valueOf(null)导致空指针异常?

播放2.3隐式json转换导致空指针异常

空指针异常导致应用程序崩溃

SharedPreferences 检查导致布尔值的空指针异常

空指针异常导致运行时错误

变量 'loginusername' 在被赋值之前使用。运行时可能会导致空引用异常

在SherlockFragmentActivity上使用SetAdapter?(空指针异常)

使用复选框的空指针异常

使用HttpSession时获取空指针异常

使用Spring注入EntityManager(空指针异常)

使用Mockito时面临空指针异常

使用改造的响应中的空指针异常

使用比较器排序-空指针异常

如何使用SpEL避免空指针异常

使用gson解析json的空指针异常

使用imageView时,空指针异常

使用SpringMVC的JdbcTemplate的空指针异常

Android抽屉布局可能会产生空指针异常

声纳立方体“可能抛出空指针异常”:误报?

创建空列表时,什么可能导致Null引用异常?

使用空引用访问数组时出现空指针异常