private void parallelDrainQueue(int threadCount) { ExecutorService executor = Executors.newFixedThreadPool(threadCount); for (int i = 0; i < threadCount; i++) { executor.execute(new NamedRunnable("Crawler %s", i) { @Override protected void execute() { try { drainQueue(); } catch (Exception e) { e.printStackTrace(); } } }); } executor.shutdown(); }
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);
ExecutorService taskExecutor = Executors.newFixedThreadPool(4); while(...) { taskExecutor.execute(new MyTask()); } taskExecutor.shutdown(); try { taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { ... }
ExecutorService es = Executors.newFixedThreadPool(2); List<Callable<Object>> todo = new ArrayList<Callable<Object>>(singleTable.size()); for (DataTable singleTable: uniquePhrases) { todo.add(Executors.callable(new ComputeDTask(singleTable))); } List<Future<Object>> answers = es.invokeAll(todo);
@Test public void testParallelCompilationWithMultipleQuotasAndClientSessionConcurrency() throws Exception { conf.setBoolVar(HIVE_SERVER2_PARALLEL_COMPILATION, true); conf.setIntVar(HIVE_SERVER2_PARALLEL_COMPILATION_LIMIT, 2); initDriver(conf, 10); List<CommandProcessorResponse> responseList = new ArrayList<>(); List<Callable<List<CommandProcessorResponse>>> callables = new ArrayList<>(); for (int i = 0; i < 5; i++) { callables.add(() -> compileAndRespond(true, 2)); } ExecutorService pool = Executors.newFixedThreadPool(callables.size()); try { List<Future<List<CommandProcessorResponse>>> futures = pool.invokeAll(callables); for (Future<List<CommandProcessorResponse>> future : futures) { responseList.addAll(future.get()); } } finally { pool.shutdown(); } verifyThatWaitingCompileOpsCountIsEqualTo(0); verifyThatTimedOutCompileOpsCountIsZero(responseList); verifyThatConcurrentCompilationWasIndeed(responseList); }
/** * Starts streamer. * * @throws IgniteException If failed. */ public void start() { A.notNull(getStreamer(), "streamer"); A.notNull(getIgnite(), "ignite"); A.notNull(topics, "topics"); A.notNull(consumerCfg, "kafka consumer config"); A.ensure(threads > 0, "threads > 0"); A.ensure(null != getSingleTupleExtractor() || null != getMultipleTupleExtractor(), "Extractor must be configured"); log = getIgnite().log(); executor = Executors.newFixedThreadPool(threads); IntStream.range(0, threads).forEach(i -> consumerTasks.add(new ConsumerTask(consumerCfg))); for (ConsumerTask task : consumerTasks) executor.submit(task); }
private void initialize() { System.out.println("=== Initializing Resources ==="); ExecutorService executor = Executors.newFixedThreadPool(3); executor.execute(initResource1); executor.execute(initResource2); executor.execute(initResource3); executor.shutdown(); }
public static void main(String args[]) throws InterruptedException { ExecutorService executor = Executors.newFixedThreadPool(1); executor.submit(new Runnable() { @Override public void run() { while (true) { if (Thread.currentThread().isInterrupted()) break; } } }); executor.shutdownNow(); if (!executor.awaitTermination(100, TimeUnit.MICROSECONDS)) { System.out.println("Still waiting..."); System.exit(0); } System.out.println("Exiting normally..."); }
ExecutorService pool = Executors.newFixedThreadPool(10); List<Callable<String>> tasks = new ArrayList<>(); tasks.add(new Callable<String>() { public String call() throws Exception { Thread.sleep((new Random().nextInt(5000)) + 500); return "Hello world"; } }); List<Future<String>> results = pool.invokeAll(tasks); for (Future<String> future : results) { System.out.println(future.get()); } pool.shutdown();
public ConcurrentTestRunner(Class<?> type) throws InitializationError { super(type); String numThreadsProp = System.getProperty("test.concurrency.num.threads"); if (numThreadsProp != null) { numThreads = Integer.parseInt(numThreadsProp); } setScheduler(new ConcurrentScheduler(newFixedThreadPool(numThreads, new ConcurrentTestRunnerThreadFactory()))); System.err.println(">>> ConcurrenTestRunner initialize with " + numThreads + " threads"); System.err.flush(); }
ExecutorService exec = Executors.newFixedThreadPool(50); final Future f = exec.submit(task1); exec.submit(new Runnable() { @Override public void run() { f.get(); // do stuff } });
// create a thread pool with 10 threads, this can be optimized to your hardware ExecutorService threadPool = Executors.newFixedThreadPool(10); // submit your handlers to the thread-pool for (PCHandler handler : handlersToDo) { threadPool.submit(handler); } // once we have submitted all jobs to the thread pool, it should be shutdown threadPool.shutdown(); ...
@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); } }
@Test public void testMultithreadedXmlParsing() throws InterruptedException, ExecutionException { Callable<Configuration> parserTask = () -> new XmlConfiguration(XmlConfigurationTest.class.getResource("/configs/one-cache.xml")); ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); try { for (Future<Configuration> c : service.invokeAll(nCopies(10, parserTask))) { assertThat(c.get(), IsNull.notNullValue()); } } finally { service.shutdown(); } }
public void initializeWorkers(int workers) { if (workers < 1) { errorCallback.accept(new IllegalArgumentException("Invalid number of workers: " + workers)); return; } executorService = Executors.newFixedThreadPool(workers); for (int i = 0; i < workers; i++) { WorkerRunner workerRunner = new WorkerRunner(); executorService.submit(workerRunner); } }
final ExecutorService pool = Executors.newFixedThreadPool(2); final List<? extends Callable<String>> callables = Arrays.asList( new SleepingCallable("quick", 500), new SleepingCallable("slow", 5000)); try { for (final Future<String> future : pool.invokeAll(callables)) { System.out.println(future.get()); } } catch (ExecutionException | InterruptedException ex) { } pool.shutdown();
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(); } }
import java.util.concurrent.*; class ThreadIdTest { public static void main(String[] args) { final int numThreads = 5; ExecutorService exec = Executors.newFixedThreadPool(numThreads); for (int i=0; i<10; i++) { exec.execute(new Runnable() { public void run() { long threadId = Thread.currentThread().getId(); System.out.println("I am thread " + threadId + " of " + numThreads); } }); } exec.shutdown(); } }
public static void main(String args[]) throws InterruptedException { ExecutorService executor = Executors.newFixedThreadPool(1); executor.submit(new Runnable() { @Override public void run() { try { while (true) {Thread.sleep(10);} } catch (InterruptedException e) { //ok let's get out of here } } }); executor.shutdownNow(); if (!executor.awaitTermination(100, TimeUnit.MICROSECONDS)) { System.out.println("Still waiting..."); System.exit(0); } System.out.println("Exiting normally..."); }
@Test public void scanExecutorAttributes() { Scheduler fixedThreadPool = Schedulers.fromExecutorService(Executors.newFixedThreadPool(3)); Long test = Integer.MAX_VALUE + 1L; System.out.println(test.intValue() == Integer.MAX_VALUE); assertThat(Scannable.from(fixedThreadPool).scan(Scannable.Attr.CAPACITY)).isEqualTo(3); assertThat(Scannable.from(fixedThreadPool).scan(Scannable.Attr.BUFFERED)).isZero(); assertThat(Scannable.from(fixedThreadPool).scan(Scannable.Attr.LARGE_BUFFERED)).isZero(); fixedThreadPool.schedule(() -> { assertThat(Scannable.from(fixedThreadPool).scan(Scannable.Attr.BUFFERED)).isNotZero(); assertThat(Scannable.from(fixedThreadPool).scan(Scannable.Attr.LARGE_BUFFERED)).isNotZero(); }); } }