Here are the examples of the java api class java.util.concurrent.RejectedExecutionHandler taken from open source projects.
1. ExecutionEngineImpl#activate()
Project: sling
File: ExecutionEngineImpl.java
File: ExecutionEngineImpl.java
protected void activate(ComponentContext context) { corePoolSize = getIntegerProperty(context, PROP_CORE_POOL_SIZE); maximumPoolSize = getIntegerProperty(context, PROP_MAX_POOL_SIZE); keepAliveTimeSeconds = getIntegerProperty(context, PROP_KEEP_ALIVE_TIME); TimeUnit unit = TimeUnit.SECONDS; BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(4); RejectedExecutionHandler handler = new RejectedExecutionHandler() { public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { onJobRejected(r); } }; log.info("ThreadPoolExecutor configuration: corePoolSize = {}, maxPoolSize={}, keepAliveTimeSeconds={}", new Object[] { corePoolSize, maximumPoolSize, keepAliveTimeSeconds }); executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTimeSeconds, unit, workQueue, handler); }
2. DefaultCommunicationClientImpl#initial()
Project: otter
File: DefaultCommunicationClientImpl.java
File: DefaultCommunicationClientImpl.java
public void initial() { RejectedExecutionHandler handler = null; if (discard) { handler = new ThreadPoolExecutor.DiscardPolicy(); } else { handler = new ThreadPoolExecutor.AbortPolicy(); } executor = new ThreadPoolExecutor(poolSize, poolSize, 60 * 1000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10 * 1000), new NamedThreadFactory("communication-async"), handler); }
3. ThreadPoolExecutorTest#testCallerRunsOnShutdown()
Project: openjdk
File: ThreadPoolExecutorTest.java
File: ThreadPoolExecutorTest.java
/** * execute using CallerRunsPolicy drops task on shutdown */ public void testCallerRunsOnShutdown() { RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); try { p.shutdown(); } catch (SecurityException ok) { return; } try (PoolCleaner cleaner = cleaner(p)) { TrackedNoOpRunnable r = new TrackedNoOpRunnable(); p.execute(r); assertFalse(r.done); } }
4. LoadSheddingStreamExecutorServiceFactory#create()
Project: incubator-s4
File: LoadSheddingStreamExecutorServiceFactory.java
File: LoadSheddingStreamExecutorServiceFactory.java
@Override public ExecutorService create(int parallelism, final String name, final ClassLoader classLoader) { ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("stream-" + name + "-%d").setThreadFactory(new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setContextClassLoader(classLoader); return t; } }).build(); RejectedExecutionHandler rejectedExecutionHandler = new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { metrics.droppedEvent(name); } }; ThreadPoolExecutor tpe = new ThreadPoolExecutor(parallelism, parallelism, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(workQueueSize), threadFactory, rejectedExecutionHandler); tpe.allowCoreThreadTimeOut(true); return tpe; }
5. LoadSheddingSenderExecutorServiceFactory#create()
Project: incubator-s4
File: LoadSheddingSenderExecutorServiceFactory.java
File: LoadSheddingSenderExecutorServiceFactory.java
@Override public ExecutorService create() { boolean remote = (this instanceof RemoteSendersExecutorServiceFactory); ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat(remote ? "remote-sender-%d" : "sender-%d").build(); RejectedExecutionHandler rejectedExecutionHandler = (remote ? new RejectedExecutionHandler() { // from a remote sender @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { metrics.droppedEventInRemoteSender(); } } : new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { metrics.droppedEventInSender(); } }); ThreadPoolExecutor tpe = new ThreadPoolExecutor(parallelism, parallelism, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(workQueueSize), threadFactory, rejectedExecutionHandler); tpe.allowCoreThreadTimeOut(true); return tpe; }
6. Utils#newBlockingFixedThreadPoolExecutor()
Project: deep-spark
File: Utils.java
File: Utils.java
/** * Returns an instance of ThreadPoolExecutor using an bounded queue and blocking when the worker queue is full. * @param nThreads thread pool size * @param queueSize workers queue size * @return thread pool executor */ public static ExecutorService newBlockingFixedThreadPoolExecutor(int nThreads, int queueSize) { BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(queueSize); RejectedExecutionHandler blockingRejectedExecutionHandler = new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) { try { executor.getQueue().put(task); } catch (InterruptedException e) { } } }; return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, blockingQueue, blockingRejectedExecutionHandler); }
7. DefaultThreadPoolFactory#newScheduledThreadPool()
Project: camel
File: DefaultThreadPoolFactory.java
File: DefaultThreadPoolFactory.java
@Override public ScheduledExecutorService newScheduledThreadPool(ThreadPoolProfile profile, ThreadFactory threadFactory) { RejectedExecutionHandler rejectedExecutionHandler = profile.getRejectedExecutionHandler(); if (rejectedExecutionHandler == null) { rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy(); } ScheduledThreadPoolExecutor answer = new RejectableScheduledThreadPoolExecutor(profile.getPoolSize(), threadFactory, rejectedExecutionHandler); answer.setRemoveOnCancelPolicy(true); // we could potentially keep adding tasks, and run out of memory. if (profile.getMaxPoolSize() > 0) { return new SizedScheduledExecutorService(answer, profile.getMaxQueueSize()); } else { return answer; } }
8. TelemetryClient#configThreadPool()
Project: ApplicationInsights-Android
File: TelemetryClient.java
File: TelemetryClient.java
private void configThreadPool() { int corePoolSize = 5; int maxPoolSize = 10; int queueSize = 5; long timeout = 1; ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(queueSize); ThreadFactory threadFactory = new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r); thread.setDaemon(false); return thread; } }; RejectedExecutionHandler handler = new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { InternalLogging.error(TAG, "too many track() calls at a time"); } }; threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, timeout, TimeUnit.SECONDS, queue, threadFactory, handler); }
9. Throws#realMain()
Project: openjdk
File: Throws.java
File: Throws.java
private static void realMain(String[] args) throws Throwable { final ThreadFactory fac = defaultThreadFactory(); final ThreadFactory nullFactory = null; final RejectedExecutionHandler reh = new RejectedExecutionHandler() { public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { } }; final RejectedExecutionHandler nullHandler = null; THROWS(NullPointerException.class, () -> newFixedThreadPool(3, null), () -> newCachedThreadPool(null), () -> newSingleThreadScheduledExecutor(null), () -> newScheduledThreadPool(0, null), () -> unconfigurableExecutorService(null), () -> unconfigurableScheduledExecutorService(null), () -> callable(null, "foo"), () -> callable((Runnable) null), () -> callable((PrivilegedAction<?>) null), () -> callable((PrivilegedExceptionAction<?>) null), () -> privilegedCallable((Callable<?>) null), () -> new ScheduledThreadPoolExecutor(0, nullFactory), () -> new ScheduledThreadPoolExecutor(0, nullFactory, reh), () -> new ScheduledThreadPoolExecutor(0, fac, nullHandler)); THROWS(IllegalArgumentException.class, () -> newFixedThreadPool(-42), () -> newFixedThreadPool(0), () -> newFixedThreadPool(-42, fac), () -> newFixedThreadPool(0, fac), () -> newScheduledThreadPool(-42), () -> new ScheduledThreadPoolExecutor(-42), () -> new ScheduledThreadPoolExecutor(-42, reh), () -> new ScheduledThreadPoolExecutor(-42, fac, reh)); try { newFixedThreadPool(1).shutdownNow(); pass(); } catch (Throwable t) { unexpected(t); } try { newFixedThreadPool(1, fac).shutdownNow(); pass(); } catch (Throwable t) { unexpected(t); } try { newSingleThreadExecutor().shutdownNow(); pass(); } catch (Throwable t) { unexpected(t); } try { newCachedThreadPool().shutdownNow(); pass(); } catch (Throwable t) { unexpected(t); } try { newSingleThreadScheduledExecutor().shutdownNow(); pass(); } catch (Throwable t) { unexpected(t); } try { newSingleThreadScheduledExecutor(fac).shutdownNow(); pass(); } catch (Throwable t) { unexpected(t); } try { newScheduledThreadPool(0).shutdownNow(); pass(); } catch (Throwable t) { unexpected(t); } try { newScheduledThreadPool(0, fac).shutdownNow(); pass(); } catch (Throwable t) { unexpected(t); } try { new ScheduledThreadPoolExecutor(0).shutdownNow(); pass(); } catch (Throwable t) { unexpected(t); } try { new ScheduledThreadPoolExecutor(0, fac).shutdownNow(); pass(); } catch (Throwable t) { unexpected(t); } try { new ScheduledThreadPoolExecutor(0, fac, reh).shutdownNow(); pass(); } catch (Throwable t) { unexpected(t); } }
10. ConsistencyFixTest#badKeyReaderHelper()
Project: voldemort
File: ConsistencyFixTest.java
File: ConsistencyFixTest.java
/** * * @param orphan true for testing orphan, false for testing normal... */ public void badKeyReaderHelper(boolean orphan) { String tmpDir = TestUtils.createTempDir().getAbsolutePath(); String fileName = tmpDir + "BadKeyFile"; if (orphan) { badKeyReaderWriteOrphanKeys(fileName, true); } else { badKeyReaderWriteBadKeys(fileName, true); } // Get cluster bootstrap url String url = setUpCluster(); // Construct ConsistencyFix with parseOnly true ConsistencyFix consistencyFix = new ConsistencyFix(url, STORE_NAME, 100, 100, false, true); // Do set up for BadKeyReader akin to consistencyFix.execute... int parallelism = 1; BlockingQueue<Runnable> blockingQ = new ArrayBlockingQueue<Runnable>(parallelism); RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy(); ExecutorService consistencyFixWorkers = new ThreadPoolExecutor(parallelism, parallelism, 0L, TimeUnit.MILLISECONDS, blockingQ, rejectedExecutionHandler); BlockingQueue<BadKeyStatus> badKeyQOut = new ArrayBlockingQueue<BadKeyStatus>(10000); ExecutorService badKeyReaderService = Executors.newSingleThreadExecutor(); CountDownLatch allBadKeysReadLatch = new CountDownLatch(1); // Submit file of bad keys to (appropriate) BadKeyReader BadKeyReader bkr = null; if (orphan) { bkr = new BadKeyOrphanReader(allBadKeysReadLatch, fileName, consistencyFix, consistencyFixWorkers, badKeyQOut); } else { bkr = new BadKeyReader(allBadKeysReadLatch, fileName, consistencyFix, consistencyFixWorkers, badKeyQOut); } badKeyReaderService.submit(bkr); // Wait for file to be processed. try { allBadKeysReadLatch.await(); badKeyReaderService.shutdown(); consistencyFixWorkers.shutdown(); } catch (InterruptedException e) { e.printStackTrace(); fail("Unexpected exception"); } consistencyFix.close(); // Make sure everything worked as expected. assertFalse(bkr.hasException()); assertEquals(0, badKeyQOut.size()); }
11. ConsistencyFix#execute()
Project: voldemort
File: ConsistencyFix.java
File: ConsistencyFix.java
public String execute(int parallelism, String badKeyFileIn, boolean orphanFormat, String badKeyFileOut) { ExecutorService badKeyReaderService; ExecutorService badKeyWriterService; ExecutorService consistencyFixWorkers; // Create BadKeyWriter thread BlockingQueue<BadKeyStatus> badKeyQOut = new ArrayBlockingQueue<BadKeyStatus>(parallelism * 10); badKeyWriterService = Executors.newSingleThreadExecutor(); badKeyWriterService.submit(new BadKeyWriter(badKeyFileOut, badKeyQOut)); logger.info("Created badKeyWriter."); // Create ConsistencyFixWorker thread pool BlockingQueue<Runnable> blockingQ = new ArrayBlockingQueue<Runnable>(parallelism); RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy(); consistencyFixWorkers = new ThreadPoolExecutor(parallelism, parallelism, 0L, TimeUnit.MILLISECONDS, blockingQ, rejectedExecutionHandler); logger.info("Created ConsistencyFixWorker pool."); // Create BadKeyReader thread CountDownLatch allBadKeysReadLatch = new CountDownLatch(1); badKeyReaderService = Executors.newSingleThreadExecutor(); BadKeyReader badKeyReader = null; if (!orphanFormat) { badKeyReader = new BadKeyReader(allBadKeysReadLatch, badKeyFileIn, this, consistencyFixWorkers, badKeyQOut); } else { badKeyReader = new BadKeyOrphanReader(allBadKeysReadLatch, badKeyFileIn, this, consistencyFixWorkers, badKeyQOut); } badKeyReaderService.submit(badKeyReader); logger.info("Created badKeyReader."); try { allBadKeysReadLatch.await(); badKeyReaderService.shutdown(); badKeyReaderService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); logger.info("Bad key reader service has shutdown."); consistencyFixWorkers.shutdown(); consistencyFixWorkers.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); logger.info("All workers have shutdown."); // Poison the bad key writer to have it exit. badKeyQOut.put(new BadKeyStatus()); badKeyWriterService.shutdown(); badKeyWriterService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); logger.info("Bad key writer service has shutdown."); } catch (InterruptedException e) { logger.error("InterruptedException caught."); if (logger.isDebugEnabled()) { e.printStackTrace(); } } finally { adminClient.close(); } // Cobble together a status string for overall execution. StringBuilder sb = new StringBuilder(); sb.append("\n\n"); sb.append("Exit statuses of various threads:\n"); sb.append("\tBadKeyReader: "); if (badKeyReader.hasException()) { sb.append("Had exception!\n"); } else { sb.append("OK.\n"); } sb.append("\tBadKeyWriter: "); if (badKeyReader.hasException()) { sb.append("Had exception!\n"); } else { sb.append("OK.\n"); } sb.append("\n\n"); sb.append(stats.summary()); return sb.toString(); }
12. ThreadPoolExecutorTest#testGetRejectedExecutionHandler()
Project: openjdk
File: ThreadPoolExecutorTest.java
File: ThreadPoolExecutorTest.java
/** * getRejectedExecutionHandler returns handler in constructor if not set */ public void testGetRejectedExecutionHandler() { final RejectedExecutionHandler handler = new NoOpREHandler(); final ThreadPoolExecutor p = new ThreadPoolExecutor(1, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), handler); try (PoolCleaner cleaner = cleaner(p)) { assertSame(handler, p.getRejectedExecutionHandler()); } }
13. ThreadPoolExecutorSubclassTest#testGetRejectedExecutionHandler()
Project: openjdk
File: ThreadPoolExecutorSubclassTest.java
File: ThreadPoolExecutorSubclassTest.java
/** * getRejectedExecutionHandler returns handler in constructor if not set */ public void testGetRejectedExecutionHandler() { final RejectedExecutionHandler handler = new NoOpREHandler(); final ThreadPoolExecutor p = new CustomTPE(1, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), handler); try (PoolCleaner cleaner = cleaner(p)) { assertSame(handler, p.getRejectedExecutionHandler()); } }
14. ThreadPoolMXBeanAdapter#getRejectedExecutionHandlerClass()
Project: ignite
File: ThreadPoolMXBeanAdapter.java
File: ThreadPoolMXBeanAdapter.java
/** {@inheritDoc} */ @Override public String getRejectedExecutionHandlerClass() { if (!(exec instanceof ThreadPoolExecutor)) return ""; RejectedExecutionHandler hnd = ((ThreadPoolExecutor) exec).getRejectedExecutionHandler(); return hnd == null ? "" : hnd.getClass().getName(); }
15. ThreadExecutorStatsMXBeanImpl#getRejectedTaskCount()
Project: controller
File: ThreadExecutorStatsMXBeanImpl.java
File: ThreadExecutorStatsMXBeanImpl.java
@Override public Long getRejectedTaskCount() { RejectedExecutionHandler rejectedHandler = executor.getRejectedExecutionHandler(); if (rejectedHandler instanceof CountingRejectedExecutionHandler) { return Long.valueOf(((CountingRejectedExecutionHandler) rejectedHandler).getRejectedTaskCount()); } return null; }