java - 为什么 Logback#AsyncAppender 使用 ArrayBlockingQueue 而不是 LinkedBlockingQueue
问题描述
logback 的异步 appender 使用BlockingQueue
. 使用场景是multi-producer single-consumer
并且需要绑定队列。所以两者都ArrayBlockingQueue
应该LinkedBlockingQueue
满足这种情况。
这是两个队列之间的区别。(来自对这两个队列之间差异的讨论。link1,link2)
LinkedBlockingQueue
:
- 应该有更好的吞吐量,因为它对头部和尾部使用单独的锁。
ArrayBlockingQueue
:
- 应该有更好的延迟,因为在数组中设置引用会更快。
- 预分配其支持数组。
我想知道使用ArrayBlockingQueue
.
以下是 LogBack 源代码的一些片段(来自AsyncAppenderBase
):
生产 :
// The default length of the queue is 256, which is configurable
private void put(E eventObject) {
// If false (the default) the appender will block on appending to a full queue rather than losing the message. Set to true and the appender will just drop the message and will not block your application.
if (neverBlock) {
blockingQueue.offer(eventObject);
} else {
putUninterruptibly(eventObject);
}
}
消耗
while (parent.isStarted()) {
try {
E e = parent.blockingQueue.take();
aai.appendLoopOnAppenders(e);
} catch (InterruptedException ie) {
break;
}
}
}
解决方案
推荐阅读
- unity3d - 性能优化:Update() 中的空检查
- c - 为什么我不能从我的主循环中读取中断标志?
- c++ - 我不知道为什么只有一个代码在两个代码之间有 SIGSEGV(看起来一样)
- python - 如何在 x 分钟后重复停止程序并重新运行它以更改 python 中的一个变量?
- python - Django Formset:对象没有属性'get'
- angular - 从 Froala 自定义按钮调用自定义方法
- kotlin - 在 Kotlin 中为不可变数据类重用映射代码
- paypal - Paypal Refund Rest Api v2 - 由于权限不足,授权失败
- docker - 启用 VPN 后,Windows 10 上的 docker 无法挂载卷
- jquery - jQuery 中 AJAX POST 请求的全局事件处理程序