public RequestManager(int numThreads, int maxPerNode) { ExecutorService localExecutor = Executors.newFixedThreadPool(numThreads, new ThreadFactoryBuilder().setNameFormat("TaskCommunicator #%2d").build()); maxConcurrentRequestsPerNode = maxPerNode; executor = MoreExecutors.listeningDecorator(localExecutor); }
@Override protected ScheduledExecutorService executor() { final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor( new ThreadFactoryBuilder() .setDaemon(true) .setNameFormat(serviceName()) .build()); // Add a listener to shutdown the executor after the service is stopped. This ensures that the // JVM shutdown will not be prevented from exiting after this service has stopped or failed. // Technically this listener is added after start() was called so it is a little gross, but it // is called within doStart() so we know that the service cannot terminate or fail concurrently // with adding this listener so it is impossible to miss an event that we are interested in. addListener(new Listener() { @Override public void terminated(State from) { executor.shutdown(); } @Override public void failed(State from, Throwable failure) { executor.shutdown(); }}, MoreExecutors.directExecutor()); return executor; }
pool = MoreExecutors.sameThreadExecutor(); } else { pool = Executors.newFixedThreadPool(threads, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("Get-Partitions-Size-%d").build());
public void testThreadRenaming() { Executor renamingExecutor = renamingDecorator(newDirectExecutorService(), Suppliers.ofInstance("FooBar")); String oldName = Thread.currentThread().getName(); renamingExecutor.execute( new Runnable() { @Override public void run() { assertEquals("FooBar", Thread.currentThread().getName()); } }); assertEquals(oldName, Thread.currentThread().getName()); }
/** invokeAny(empty collection) throws IAE */ public void testInvokeAnyImpl_emptyTasks() throws Exception { ListeningExecutorService e = newDirectExecutorService(); try { invokeAnyImpl(e, new ArrayList<Callable<String>>(), false, 0, TimeUnit.NANOSECONDS); fail(); } catch (IllegalArgumentException success) { } finally { joinPool(e); } }
public void testListeningDecorator() throws Exception { ListeningExecutorService service = listeningDecorator(newDirectExecutorService()); assertSame(service, listeningDecorator(service)); List<Callable<String>> callables = ImmutableList.of(Callables.returning("x")); List<Future<String>> results; results = service.invokeAll(callables); assertThat(getOnlyElement(results)).isInstanceOf(TrustedListenableFutureTask.class); results = service.invokeAll(callables, 1, SECONDS); assertThat(getOnlyElement(results)).isInstanceOf(TrustedListenableFutureTask.class); /* * TODO(cpovirk): move ForwardingTestCase somewhere common, and use it to * test the forwarded methods */ }
Lists.newArrayList( new IntervalLoadRule( Intervals.of("2012-01-01T00:00:00.000Z/2012-01-01T12:00:00.000Z"), ListeningExecutorService exec = MoreExecutors.listeningDecorator( Executors.newFixedThreadPool(1)); BalancerStrategy balancerStrategy = new CostBalancerStrategyFactory().createBalancerStrategy(exec); exec.shutdown(); EasyMock.verify(emitter); EasyMock.verify(mockPeon);
private void testWithExecutor( final DimFilter filter, final List<String> expectedRows ) { ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(EXECUTOR_NUM_THREADS)); List<ListenableFuture<?>> futures = new ArrayList<>(); for (int i = 0; i < EXECUTOR_NUM_TASKS; i++) { Runnable runnable = makeFilterRunner(filter, expectedRows); ListenableFuture fut = executor.submit(runnable); futures.add(fut); } try { Futures.allAsList(futures).get(60, TimeUnit.SECONDS); } catch (Exception ex) { Assert.fail(ex.getMessage()); } executor.shutdown(); } }
final ListeningExecutorService executorService = MoreExecutors.listeningDecorator( Executors.newFixedThreadPool( concurrentThreads, new ThreadFactoryBuilder() .setDaemon(false) .setNameFormat("indexible-writes-benchmark-%d") .build() executorService.submit( new Runnable() Futures.allAsList(futures).get(); Assert.assertTrue(StringUtils.format("Index too small %d, expected %d across %d loops", index.get(), totalIndexSize, loops), index.get() >= totalIndexSize); for (int i = 0; i < index.get(); ++i) { executorService.shutdown();
@Inject public FileSingleStreamSpillerFactory(BlockEncodingSerde blockEncodingSerde, SpillerStats spillerStats, FeaturesConfig featuresConfig) { this( listeningDecorator(newFixedThreadPool( requireNonNull(featuresConfig, "featuresConfig is null").getSpillerThreads(), daemonThreadsNamed("binary-spiller-%s"))), blockEncodingSerde, spillerStats, requireNonNull(featuresConfig, "featuresConfig is null").getSpillerSpillPaths(), requireNonNull(featuresConfig, "featuresConfig is null").getSpillMaxUsedSpaceThreshold()); }
this.executorService = MoreExecutors.listeningDecorator(threadPoolExecutor); this.preemptionQueue = new PriorityBlockingQueue<>(numExecutors, new PreemptionQueueComparator()); ExecutorService wes = Executors.newFixedThreadPool(1, new ThreadFactoryBuilder() .setDaemon(true).setNameFormat(WAIT_QUEUE_SCHEDULER_THREAD_NAME_FORMAT).build()); this.waitQueueExecutorService = MoreExecutors.listeningDecorator(wes); ExecutorService executionCompletionExecutorServiceRaw = Executors.newFixedThreadPool(1, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("ExecutionCompletionThread #%d") .build()); executionCompletionExecutorService = MoreExecutors.listeningDecorator( executionCompletionExecutorServiceRaw); ListenableFuture<?> future = waitQueueExecutorService.submit(new WaitQueueWorker()); Futures.addCallback(future, new WaitQueueWorkerCallback());
ListeningExecutorService createExecutorService(Closer closer) { final ListeningExecutorService executorService = MoreExecutors.listeningDecorator( Executors.newCachedThreadPool( new ThreadFactoryBuilder().setNameFormat(serviceName + "-worker-%d").build())); closer.register(asCloseable(executorService)); return executorService; }
public CeProcessingSchedulerExecutorServiceImpl(CeConfiguration ceConfiguration) { super( MoreExecutors.listeningDecorator( Executors.newScheduledThreadPool(ceConfiguration.getWorkerMaxCount(), new ThreadFactoryBuilder() .setNameFormat(THREAD_NAME_PREFIX + "%d") .setPriority(Thread.MIN_PRIORITY) .build()))); }
public void start(long time) throws InterruptedException, TimeoutException, ExecutionException { List<Callable<Object>> all = new ArrayList<>(); for (int i = 0; i < writerThreads; i++) { all.add(Executors.callable(new Writer())); } for (int i = 0; i < readerThreads; i++) { all.add(Executors.callable(new Reader())); } for (int i = 0; i < deleterThreads; i++) { all.add(Executors.callable(new Deleter())); } for (int i = 0; i < flusherThreads; i++) { all.add(Executors.callable(new Flusher())); } ExecutorService executor = Executors.newFixedThreadPool(all.size()); ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(executor); AnyFuture<Object> anyFuture = new AnyFuture<>(); for (Callable<Object> callable : all) { ListenableFuture<Object> future = listeningExecutorService.submit(callable); anyFuture.add(future); } try { anyFuture.get(time, TimeUnit.SECONDS); } catch (TimeoutException e) { System.out.println("Passed."); } stopped = true; } }
@Test public void interruptTest() throws InterruptedException { ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()); final ListenableFuture<Object> future = executor.submit(() -> { // try { System.out.println("Waiting"); Thread.sleep(10000); System.out.println("Complete"); return null; // } catch (Exception e) { // e.printStackTrace(); // throw e; // } }); future.addListener(() -> { System.out.println("Listener: " + future.isCancelled() + ", " + future.isDone()); try { future.get(); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (ExecutionException e) { throw new RuntimeException(e); } }, MoreExecutors.directExecutor()); Thread.sleep(1000); future.cancel(true); }
@BeforeClass public void setUp() { executor = MoreExecutors.listeningDecorator(newCachedThreadPool()); }
@BeforeMethod public void setUp() { executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); closer.register(() -> executor.shutdownNow()); spillPath1 = Files.createTempDir(); closer.register(() -> deleteRecursively(spillPath1.toPath(), ALLOW_INSECURE)); spillPath2 = Files.createTempDir(); closer.register(() -> deleteRecursively(spillPath2.toPath(), ALLOW_INSECURE)); }
@Override protected void setUp() throws Exception { super.setUp(); exec = Executors.newCachedThreadPool(); task.addListener( new Runnable() { @Override public void run() { listenerLatch.countDown(); } }, directExecutor()); }
public void testTransformAsync_cancelPropagatesToInput() throws Exception { SettableFuture<Foo> input = SettableFuture.create(); AsyncFunction<Foo, Bar> function = new AsyncFunction<Foo, Bar>() { @Override public ListenableFuture<Bar> apply(Foo unused) { throw new AssertionFailedError("Unexpeted call to apply."); } }; assertTrue(transformAsync(input, function, directExecutor()).cancel(false)); assertTrue(input.isCancelled()); assertFalse(input.wasInterrupted()); }
public void testSameThreadSuccess() { SettableFuture<String> f = SettableFuture.create(); MockCallback callback = new MockCallback("foo"); addCallback(f, callback, directExecutor()); f.set("foo"); }