From 333f45ccd5f65bf64aa6e52dfa0ed080cfd17486 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Sun, 16 Nov 2008 15:02:01 +0000 Subject: [PATCH] Made sure events are propagated somehow. --- .../MemoryAwareThreadPoolExecutor.java | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/jboss/netty/handler/execution/MemoryAwareThreadPoolExecutor.java b/src/main/java/org/jboss/netty/handler/execution/MemoryAwareThreadPoolExecutor.java index 22b44d9f8a..97426e903e 100644 --- a/src/main/java/org/jboss/netty/handler/execution/MemoryAwareThreadPoolExecutor.java +++ b/src/main/java/org/jboss/netty/handler/execution/MemoryAwareThreadPoolExecutor.java @@ -27,6 +27,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.Executor; import java.util.concurrent.Executors; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.Semaphore; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; @@ -83,9 +85,9 @@ import org.jboss.netty.util.LinkedTransferQueue; */ public class MemoryAwareThreadPoolExecutor extends ThreadPoolExecutor { - private static final InternalLogger logger = + private static final InternalLogger logger = InternalLoggerFactory.getInstance(MemoryAwareThreadPoolExecutor.class); - + private volatile Settings settings = new Settings(0, 0); // XXX Can be changed in runtime now. Make it mutable in 3.1. @@ -167,7 +169,8 @@ public class MemoryAwareThreadPoolExecutor extends ThreadPoolExecutor { long keepAliveTime, TimeUnit unit, ObjectSizeEstimator objectSizeEstimator, ThreadFactory threadFactory) { - super(corePoolSize, corePoolSize, keepAliveTime, unit, new LinkedTransferQueue(), threadFactory); + super(corePoolSize, corePoolSize, keepAliveTime, unit, + new LinkedTransferQueue(), threadFactory, new NewThreadRunsPolicy()); if (objectSizeEstimator == null) { throw new NullPointerException("objectSizeEstimator"); @@ -403,4 +406,20 @@ public class MemoryAwareThreadPoolExecutor extends ThreadPoolExecutor { this.maxTotalMemorySize = maxTotalMemorySize; } } + + private static class NewThreadRunsPolicy implements RejectedExecutionHandler { + NewThreadRunsPolicy() { + super(); + } + + public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { + try { + final Thread t = new Thread(r, "Temporary task executor"); + t.start(); + } catch (Throwable e) { + throw new RejectedExecutionException( + "Failed to start a new thread", e); + } + } + } }