You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by mi...@apache.org on 2015/11/18 17:13:10 UTC
[08/20] logging-log4j2 git commit: AsyncLoggerConfigDisruptor change:
Determining if the current thread is the background thread is now done by
comparing thread IDs, without requiring a ThreadLocal.
AsyncLoggerConfigDisruptor change: Determining if the current thread is
the background thread is now done by comparing thread IDs, without
requiring a ThreadLocal.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/8af46ccd
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/8af46ccd
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/8af46ccd
Branch: refs/heads/LOG4J-1181
Commit: 8af46ccdd01336874634e6cdb20d1fd980e9c545
Parents: 56bb090
Author: rpopma <rp...@apache.org>
Authored: Wed Nov 11 21:33:05 2015 +0900
Committer: rpopma <rp...@apache.org>
Committed: Wed Nov 11 21:33:05 2015 +0900
----------------------------------------------------------------------
.../core/async/AsyncLoggerConfigDisruptor.java | 30 ++++++++------------
1 file changed, 12 insertions(+), 18 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/8af46ccd/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java
index 846782c..92ad9a0 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java
@@ -131,10 +131,10 @@ public class AsyncLoggerConfigDisruptor implements AsyncLoggerConfigDelegate {
};
private static final ThreadFactory THREAD_FACTORY = new DaemonThreadFactory("AsyncLoggerConfig-");
- private static final ThreadLocal<Boolean> IS_APPENDER_THREAD = new ThreadLocal<>();
private volatile Disruptor<Log4jEventWrapper> disruptor;
private ExecutorService executor;
+ private long backgroundThreadId; // LOG4J2-471
public AsyncLoggerConfigDisruptor() {
}
@@ -155,7 +155,7 @@ public class AsyncLoggerConfigDisruptor implements AsyncLoggerConfigDelegate {
final int ringBufferSize = DisruptorUtil.calculateRingBufferSize("AsyncLoggerConfig.RingBufferSize");
final WaitStrategy waitStrategy = DisruptorUtil.createWaitStrategy("AsyncLoggerConfig.WaitStrategy");
executor = Executors.newSingleThreadExecutor(THREAD_FACTORY);
- initThreadLocalForExecutorThread(executor);
+ backgroundThreadId = DisruptorUtil.getExecutorThreadId(executor);
disruptor = new Disruptor<>(FACTORY, ringBufferSize, executor, ProducerType.MULTI, waitStrategy);
@@ -211,21 +211,6 @@ public class AsyncLoggerConfigDisruptor implements AsyncLoggerConfigDelegate {
return !ringBuffer.hasAvailableCapacity(ringBuffer.getBufferSize());
}
- /**
- * Initialize the threadlocal that allows us to detect Logger.log() calls initiated from the appender thread, which
- * may cause deadlock when the RingBuffer is full. (LOG4J2-471)
- *
- * @param executor contains the appender background thread
- */
- private static void initThreadLocalForExecutorThread(final ExecutorService executor) {
- executor.submit(new Runnable() {
- @Override
- public void run() {
- IS_APPENDER_THREAD.set(Boolean.TRUE);
- }
- });
- }
-
/*
* (non-Javadoc)
*
@@ -303,7 +288,16 @@ public class AsyncLoggerConfigDisruptor implements AsyncLoggerConfigDelegate {
* Returns true if the specified ringbuffer is full and the Logger.log() call was made from the appender thread.
*/
private boolean isCalledFromAppenderThreadAndBufferFull(Disruptor<Log4jEventWrapper> theDisruptor) {
- return IS_APPENDER_THREAD.get() == Boolean.TRUE && theDisruptor.getRingBuffer().remainingCapacity() == 0;
+ return currentThreadIsAppenderThread() && theDisruptor.getRingBuffer().remainingCapacity() == 0;
+ }
+
+ /**
+ * Returns {@code true} if the current thread is the Disruptor background thread, {@code false} otherwise.
+ *
+ * @return whether this thread is the Disruptor background thread.
+ */
+ private boolean currentThreadIsAppenderThread() {
+ return Thread.currentThread().getId() == backgroundThreadId;
}
/*