Class AsyncDrainWriter

java.lang.Object
org.apache.maven.logging.AsyncDrainWriter
All Implemented Interfaces:
AutoCloseable, Consumer<String>

public class AsyncDrainWriter extends Object implements Consumer<String>, AutoCloseable
A lock-free buffering wrapper around a Consumer<String> that eliminates contention when multiple threads log concurrently.

Callers enqueue messages into a ConcurrentLinkedQueue (lock-free), then attempt a non-blocking drain via ReentrantLock.tryLock(). If another thread is already draining, the caller returns immediately — its message will be picked up by the ongoing or next drain cycle. This ensures that at most one thread writes to the underlying consumer at any time, without blocking producers.

The pattern eliminates the synchronized contention in PrintWriter.println(String) that occurs when multiple PhasingExecutor threads log during parallel model building.

Since:
4.0.0
  • Constructor Details

    • AsyncDrainWriter

      public AsyncDrainWriter(Consumer<String> delegate)
  • Method Details

    • accept

      public void accept(String msg)
      Specified by:
      accept in interface Consumer<String>
    • close

      public void close()
      Flushes all remaining buffered messages to the delegate. Blocks until the drain is complete — call this before shutdown to ensure no messages are lost.
      Specified by:
      close in interface AutoCloseable