From 69d136456737193aea0b8e8303fb10fc861649c1 Mon Sep 17 00:00:00 2001 From: Eugene Zhuravlev Date: Tue, 13 May 2014 18:50:44 +0400 Subject: [PATCH] batching tasks to reduce the number of events posted to EDT --- .../execution/junit2/segments/Extractor.java | 44 +++++++++++++------ 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/java/execution/impl/src/com/intellij/execution/junit2/segments/Extractor.java b/java/execution/impl/src/com/intellij/execution/junit2/segments/Extractor.java index fb42fe5eefd4..50affa1c68bb 100644 --- a/java/execution/impl/src/com/intellij/execution/junit2/segments/Extractor.java +++ b/java/execution/impl/src/com/intellij/execution/junit2/segments/Extractor.java @@ -28,17 +28,24 @@ import javax.swing.*; import java.io.InputStream; import java.io.Reader; import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.BlockingQueue; import java.util.concurrent.Executor; +import java.util.concurrent.LinkedBlockingQueue; /** * @author dyoma */ public class Extractor { + private static final int MAX_TASKS_TO_PROCESS_AT_ONCE = 100; + private DeferredActionsQueue myFulfilledWorkGate = null; private final SegmentedInputStream myStream; private OutputPacketProcessor myEventsDispatcher; private static final Logger LOG = Logger.getInstance("#" + Extractor.class.getName()); - private final Executor myQueue2 = new SequentialTaskExecutor(PooledThreadExecutor.INSTANCE); + private final Executor myExecutor = new SequentialTaskExecutor(PooledThreadExecutor.INSTANCE); + private final BlockingQueue myTaskQueue = new LinkedBlockingQueue(); public Extractor(@NotNull InputStream stream, @NotNull Charset charset) { myStream = new SegmentedInputStream(stream, charset); @@ -86,19 +93,30 @@ public class Extractor { } private void scheduleTask(final DeferredActionsQueue queue, final Runnable task) { - myQueue2.execute(new Runnable() { + myTaskQueue.add(task); + myExecutor.execute(new Runnable() { public void run() { - // for some reason there is a requirement that this activity must be done from the swing thread - // will be blocking one of pooled threads here, which is ok - try { - SwingUtilities.invokeAndWait(new Runnable() { - public void run() { - queue.addLast(task); - } - }); - } - catch (Throwable e) { - LOG.info("Task rejected: " + task, e); + final List currentTasks = new ArrayList(MAX_TASKS_TO_PROCESS_AT_ONCE); + if (myTaskQueue.drainTo(currentTasks, MAX_TASKS_TO_PROCESS_AT_ONCE) > 0) { + // there is a requirement that these activities must be run from the swing thread + // will be blocking one of pooled threads here, which is ok + try { + SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + for (Runnable task : currentTasks) { + try { + queue.addLast(task); + } + catch (Throwable e) { + LOG.info(e); + } + } + } + }); + } + catch (Throwable e) { + LOG.info("Task rejected: " + currentTasks, e); + } } } });