1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
public class EagerThreadPool extends ThreadPoolExecutor { public EagerThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); }
@Override public void execute(Runnable command) { if (command == null) { throw new NullPointerException(); } executeInternal(command, 0, TimeUnit.MILLISECONDS); }
public void executeInternal(Runnable command, long timeout, TimeUnit unit) { try { super.execute(command); } catch (RejectedExecutionException rx) { if (getQueue() instanceof TaskQueue) { final TaskQueue queue = (TaskQueue) getQueue(); try { if (!queue.forceOffer(command, timeout, unit)) { throw new RejectedExecutionException("Queue capacity is full.", rx); } } catch (InterruptedException x) { throw new RejectedExecutionException(x); } } else { throw rx; } } } }
|