final ExecutorService producers = Executors.newFixedThreadPool(100); final ExecutorService consumers = Executors.newFixedThreadPool(100); while (/* has more work */) { producers.submit(...); } producers.shutdown(); producers.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); consumers.shutdown(); consumers.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
@Test public void testGetInputPathsPool() throws IOException, ExecutionException, InterruptedException { List<Path> pathsToAdd = new ArrayList<>(); Path path = new Path("dummy-path"); pathsToAdd.add(path); pathsToAdd.add(path); pathsToAdd.add(path); ExecutorService pool = mock(ExecutorService.class); Future mockFuture = mock(Future.class); when(mockFuture.get()).thenReturn(path); when(pool.submit(any(Callable.class))).thenReturn(mockFuture); Utilities.getInputPathsWithPool(mock(JobConf.class), mock(MapWork.class), mock(Path.class), mock(Context.class), false, pathsToAdd, pool); verify(pool, times(3)).submit(any(Callable.class)); verify(pool).shutdown(); verify(pool).shutdownNow(); }
@Test public void testCheckQuotasInMultiThreads() throws InterruptedException, ExecutionException { final Metrics metrics = new Metrics(new MetricConfig().quota(Quota.upperBound(Double.MAX_VALUE)) final int threadCount = 10; final CountDownLatch latch = new CountDownLatch(1); ExecutorService service = Executors.newFixedThreadPool(threadCount); List<Future<Throwable>> workers = new ArrayList<>(threadCount); boolean needShutdown = true; for (int i = 0; i != threadCount; ++i) { final int index = i; workers.add(service.submit(new Callable<Throwable>() { @Override public Throwable call() { service.shutdown(); assertTrue(service.awaitTermination(10, TimeUnit.SECONDS)); needShutdown = false; for (Future<Throwable> callable : workers) { assertTrue("If this failure happen frequently, we can try to increase the wait time", callable.isDone()); assertNull("Sensor#checkQuotas SHOULD be thread-safe!", callable.get());
private void llapCachePurge(final SessionState ss, final LlapRegistryService llapRegistryService) throws Exception { ExecutorService executorService = Executors.newCachedThreadPool(); List<Future<Long>> futures = new ArrayList<>(); Collection<LlapServiceInstance> instances = llapRegistryService.getInstances().getAll(); for (LlapServiceInstance instance : instances) { futures.add(executorService.submit(new PurgeCallable(ss.getConf(), instance))); } int i = 0; for (LlapServiceInstance instance : instances) { Future<Long> future = futures.get(i); ss.out.println(Joiner.on("\t").join(instance.getHost(), future.get())); i++; } }
@Test public void testLoadSegment() throws ExecutionException, InterruptedException, SegmentLoadingException { final List<Future<Boolean>> futures = segments.stream() .map( segment -> executor.submit( () -> segmentManager.loadSegment(segment) ) ) .collect(Collectors.toList()); for (Future<Boolean> eachFuture : futures) { Assert.assertTrue(eachFuture.get()); } assertResult(segments); }
@Test public void testTimeoutAndRetryJoinGroupIfNeeded() throws Exception { setupCoordinator(); mockClient.prepareResponse(groupCoordinatorResponse(node, Errors.NONE)); coordinator.ensureCoordinatorReady(mockTime.timer(0)); ExecutorService executor = Executors.newFixedThreadPool(1); try { Timer firstAttemptTimer = mockTime.timer(REQUEST_TIMEOUT_MS); Future<Boolean> firstAttempt = executor.submit(() -> coordinator.joinGroupIfNeeded(firstAttemptTimer)); mockTime.sleep(REQUEST_TIMEOUT_MS); assertFalse(firstAttempt.get()); assertTrue(consumerClient.hasPendingRequests(coordinatorNode)); mockClient.respond(joinGroupFollowerResponse(1, "memberId", "leaderId", Errors.NONE)); mockClient.prepareResponse(syncGroupResponse(Errors.NONE)); Timer secondAttemptTimer = mockTime.timer(REQUEST_TIMEOUT_MS); Future<Boolean> secondAttempt = executor.submit(() -> coordinator.joinGroupIfNeeded(secondAttemptTimer)); assertTrue(secondAttempt.get()); } finally { executor.shutdownNow(); executor.awaitTermination(1000, TimeUnit.MILLISECONDS); } }
@Override public void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException { checkNotNull(runnable); checkNotNull(timeoutUnit); checkPositiveTimeout(timeoutDuration); Future<?> future = executor.submit(runnable); try { future.get(timeoutDuration, timeoutUnit); } catch (InterruptedException | TimeoutException e) { future.cancel(true /* mayInterruptIfRunning */); throw e; } catch (ExecutionException e) { wrapAndThrowRuntimeExecutionExceptionOrError(e.getCause()); throw new AssertionError(); } }
public void run() { ExecutorService service = Executors.newSingleThreadExecutor(); Callable<Object> callable = new Callable<Object>() { public Object call() throws Exception { runTestMethod(); return null; } }; Future<Object> result = service.submit(callable); service.shutdown(); try { boolean terminated = service.awaitTermination(timeout, TimeUnit.MILLISECONDS); if (!terminated) { service.shutdownNow(); } result.get(0, TimeUnit.MILLISECONDS); // throws the exception if one occurred during the invocation } catch (TimeoutException e) { addFailure(new Exception(String.format("test timed out after %d milliseconds", timeout))); } catch (Exception e) { addFailure(e); } } });
ExecutorService exec = Executors.newFixedThreadPool(SOME_NUM_OF_THREADS); try { for (final Object o : list) { exec.submit(new Runnable() { @Override public void run() { // do stuff with o. } }); } } finally { exec.shutdown(); }
@Override public java.util.concurrent.Future<AcceptVpcEndpointConnectionsResult> acceptVpcEndpointConnectionsAsync(final AcceptVpcEndpointConnectionsRequest request, final com.amazonaws.handlers.AsyncHandler<AcceptVpcEndpointConnectionsRequest, AcceptVpcEndpointConnectionsResult> asyncHandler) { final AcceptVpcEndpointConnectionsRequest finalRequest = beforeClientExecution(request); return executorService.submit(new java.util.concurrent.Callable<AcceptVpcEndpointConnectionsResult>() { @Override public AcceptVpcEndpointConnectionsResult call() throws Exception { AcceptVpcEndpointConnectionsResult result = null; try { result = executeAcceptVpcEndpointConnections(finalRequest); } catch (Exception ex) { if (asyncHandler != null) { asyncHandler.onError(ex); } throw ex; } if (asyncHandler != null) { asyncHandler.onSuccess(finalRequest, result); } return result; } }); }
ExecutorService service = Executors.newSingleThreadExecutor(); try { Runnable r = new Runnable() { @Override public void run() { // Database task } }; Future<?> f = service.submit(r); f.get(2, TimeUnit.MINUTES); // attempt the task for two minutes } catch (final InterruptedException e) { // The thread was interrupted during sleep, wait or join } catch (final TimeoutException e) { // Took too long! } catch (final ExecutionException e) { // An exception from within the Runnable task } finally { service.shutdown(); }
class TaskAsCallable implements Callable<Result> { @Override public Result call() { return a new Result() // this is where the work is done. } } ExecutorService executor = Executors.newFixedThreadPool(300); Future<Result> task = executor.submit(new TaskAsCallable()); Result result = task.get(); // this blocks until result is ready
private static void shutdownDbExecutor(ExecutorService executorService, Collection<SQLiteConnection> connections) { for (final SQLiteConnection connection : connections) { getFuture("close connection on reset", executorService.submit(new Callable<Void>() { @Override public Void call() throws Exception { connection.dispose(); return null; } })); } executorService.shutdown(); try { executorService.awaitTermination(30, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new RuntimeException(e); } }
/** Runs {@code callable} concurrently {@code numberOfThreads} times. */ @GwtIncompatible // concurrency private void runConcurrentTest(int numberOfThreads, final Callable<Void> callable) throws Exception { ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads); final CountDownLatch startLatch = new CountDownLatch(numberOfThreads); final CountDownLatch doneLatch = new CountDownLatch(numberOfThreads); for (int i = numberOfThreads; i > 0; i--) { @SuppressWarnings("unused") // go/futurereturn-lsc Future<?> possiblyIgnoredError = executorService.submit( new Callable<Void>() { @Override public Void call() throws Exception { startLatch.countDown(); startLatch.await(); callable.call(); doneLatch.countDown(); return null; } }); } doneLatch.await(); } }
ExecutorService pool = Executors.newFixedThreadPool(10); for (String name : fileNames) { pool.submit(new DownloadTask(name, toPath)); } pool.shutdown(); pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS); // all tasks have now finished (unless an exception is thrown above)
private void applyBatchesInParallel( ParallelNativeIndexPopulator<GenericKey,NativeIndexValue> populator, int batchCountPerThread ) throws ExecutionException, InterruptedException { CountDownLatch startSignal = new CountDownLatch( THREADS ); List<Future<Void>> futures = new ArrayList<>(); for ( int i = 0; i < THREADS; i++ ) { futures.add( executorService.submit( () -> { // Wait for all to get into pole position, this is because we want to make sure all threads are used. startSignal.countDown(); startSignal.await(); for ( int j = 0; j < batchCountPerThread; j++ ) { populator.add( asList( update( next.getAndIncrement() ) ) ); } return null; } ) ); } for ( Future<Void> future : futures ) { future.get(); } }