java 线程安全queue_在Java中BlockingQueue是否完全是线程安全的

小编典典

快速答案是肯定的,它们是线程安全的。但是不要让它在那里…

首先,一个小的内部管理BlockingQueue是一个接口,任何不是线程安全的实现都将破坏书面合同。您包括的链接是指LinkedBlockingQueue,它具有一定的灵巧性。

您包含的链接引起了一个有趣的观察,是的,其中有两个锁LinkedBlockingQueue。但是,它无法理解,实际上正在处理“简单”实现会被犯规的极端情况,这就是为什么take和put方法比起初期望的要复杂的原因。

LinkedBlockingQueue进行了优化,以避免在读取和写入时使用相同的锁,这减少了争用,但是对于正确的行为,它依赖于队列不为空。当队列中包含元素时,推送和弹出点不在内存的同一区域,可以避免争用。但是,当队列为空时,则无法避免争用,因此需要额外的代码来处理这种常见的“边缘”情况。这是代码复杂度和性能/可伸缩性之间的常见折衷。

接下来的问题是,如何LinkedBlockingQueue知道队列何时为空/不为空,从而处理线程呢?答案是,它使用AtomicInteger和Condition作为两个额外的并发数据结构。所述AtomicInteger用于检查队列的长度是否是零,并且条件是用于等待一个信号时,队列可能是所希望的状态,以通知一个等待的线程。这种额外的协调确实会产生开销,但是在测量中已显示,在增加并发线程数时,此技术的开销低于使用单个锁引入的争用。

下面,我从复制了代码,LinkedBlockingQueue并添加了解释它们如何工作的注释。在较高级别上,take()首先将所有其他呼叫锁定take(),然后put()根据需要发出信号。

put()以类似的方式工作,首先阻止所有其他呼叫put(),然后take()在必要时发出信号。

从put()方法:

// putLock coordinates the calls to put() only; further coordination

// between put() and take() follows below

putLock.lockInterruptibly();

try {

// block while the queue is full; count is shared between put() and take()

// and is safely visible between cores but prone to change between calls

// a while loop is used because state can change between signals, which is

// why signals get rechecked and resent.. read on to see more of that

while (count.get() == capacity) {

notFull.await();

}

// we know that the queue is not full so add

enqueue(e);

c = count.getAndIncrement();

// if the queue is not full, send a signal to wake up

// any thread that is possibly waiting for the queue to be a little

// emptier -- note that this is logically part of 'take()' but it

// has to be here because take() blocks itself

if (c + 1 < capacity)

notFull.signal();

} finally {

putLock.unlock();

}

if (c == 0)

signalNotEmpty();

从 take()

takeLock.lockInterruptibly();

try {

// wait for the queue to stop being empty

while (count.get() == 0) {

notEmpty.await();

}

// remove element

x = dequeue();

// decrement shared count

c = count.getAndDecrement();

// send signal that the queue is not empty

// note that this is logically part of put(), but

// for thread coordination reasons is here

if (c > 1)

notEmpty.signal();

} finally {

takeLock.unlock();

}

if (c == capacity)

signalNotFull();

2020-09-24

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java,有几个线程安全的集合类可以用于多线程环境下的操作。其包括Vector、Stack、HashTable等。这些集合类通过使用Synchronized来保证线程安全性。不过需要注意的是,官方已经标注Vector和HashTable为即将废弃的类,不建议使用它们。 除了上述的线程安全集合类,还有其他一些较新的线程安全集合类可以使用。其包括CopyOnWriteArrayList、CopyOnWriteArraySet和ConcurrentHashMap等。这些集合类通过特定的机制来实现线程安全性,而且在性能方面也进行了优化。 此外,Java还有Queue接口,它包含了几个线程安全的实现类,如ConcurrentLinkedQueueBlockingQueue接口的实现类。这些集合类可以在多线程环境下进行安全的队列操作。 总之,在Java有多种线程安全的集合类可供选择,可以根据具体的需求和场景选择合适的集合类来保证线程安全性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Java那些线程安全的集合类](https://blog.csdn.net/weixin_53946852/article/details/122801143)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Java线程安全的集合](https://blog.csdn.net/weixin_42601136/article/details/107108818)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值