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/d3a748e7
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/d3a748e7
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/d3a748e7

Branch: refs/heads/LOG4J2-89
Commit: d3a748e746a57be9cdec1df941a42d7cc926621d
Parents: 1c5754d
Author: rpopma <[email protected]>
Authored: Wed Nov 11 21:33:05 2015 +0900
Committer: Ralph Goers <[email protected]>
Committed: Fri Nov 20 17:39:22 2015 -0700

----------------------------------------------------------------------
 .../core/async/AsyncLoggerConfigDisruptor.java  | 30 ++++++++------------
 1 file changed, 12 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d3a748e7/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;
     }
 
     /*

Reply via email to