The logback's asynchronous appender uses BlockingQueue
. The usage scenario is multi-producer single-consumer
and requires a bound queue. So both ArrayBlockingQueue
and LinkedBlockingQueue
should satisfy this scenario.
Here are the differences between the two queues. (From discussion of the differences between these two queues. link1 , link2)
LinkedBlockingQueue
:
- Should have better throughput since it uses separate locks for the head and the tail.
ArrayBlockingQueue
:
- Should have better latency since it is faster to set reference in array.
- Pre-allocates its backing array.
I wonder what is the determining factor in using ArrayBlockingQueue
.
Here are some snippets of the LogBack source code (From AsyncAppenderBase
):
produce :
// 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);
}
}
consume
while (parent.isStarted()) {
try {
E e = parent.blockingQueue.take();
aai.appendLoopOnAppenders(e);
} catch (InterruptedException ie) {
break;
}
}
}