Java Code Examples for java.util.concurrent.ExecutorService#awaitTermination()

The following examples show how to use java.util.concurrent.ExecutorService#awaitTermination() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HelixTaskExecutor.java    From helix with Apache License 2.0 8 votes vote down vote up
private void shutdownAndAwaitTermination(ExecutorService pool) {
  LOG.info("Shutting down pool: " + pool);
  pool.shutdown(); // Disable new tasks from being submitted
  try {
    // Wait a while for existing tasks to terminate
    if (!pool.awaitTermination(200, TimeUnit.MILLISECONDS)) {
      List<Runnable> waitingTasks = pool.shutdownNow(); // Cancel currently executing tasks
      LOG.info("Tasks that never commenced execution: " + waitingTasks);
      // Wait a while for tasks to respond to being cancelled
      if (!pool.awaitTermination(200, TimeUnit.MILLISECONDS)) {
        LOG.error("Pool did not fully terminate in 200ms. pool: " + pool);
      }
    }
  } catch (InterruptedException ie) {
    // (Re-)Cancel if current thread also interrupted
    LOG.error("Interruped when waiting for shutdown pool: " + pool, ie);
    pool.shutdownNow();
    // Preserve interrupt status
    Thread.currentThread().interrupt();
  }
}
 
Example 2
Source File: AtomicAppend.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
void test(String[] args) throws Throwable {
    final int nThreads = 10;
    final int writes = 1000;
    final File file = new File("foo");
    file.delete();
    try {
        final ExecutorService es = Executors.newFixedThreadPool(nThreads);
        for (int i = 0; i < nThreads; i++)
            es.execute(new Runnable() { public void run() {
                try {
                    try (FileOutputStream s = new FileOutputStream(file, true)) {
                        for (int j = 0; j < 1000; j++) {
                            s.write((int) 'x');
                            s.flush();
                        }
                    }
                } catch (Throwable t) { unexpected(t); }}});
        es.shutdown();
        es.awaitTermination(10L, TimeUnit.MINUTES);
        equal(file.length(), (long) (nThreads * writes));
    } finally {
        file.delete();
    }
}
 
Example 3
Source File: BankTest.java    From dgraph4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBank() throws Exception {
  createAccounts();
  logger.debug(Arrays.toString(uids.toArray()));

  ExecutorService totalEx = Executors.newSingleThreadExecutor();
  totalEx.execute(this::runTotalInLoop);
  totalEx.shutdown();

  ExecutorService txnEx = Executors.newCachedThreadPool();
  for (int i = 0; i < 10; i++) {
    txnEx.execute(this::txnLoop);
  }
  txnEx.shutdown();

  if (!txnEx.awaitTermination(5, TimeUnit.MINUTES)) {
    logger.debug("Timeout elapsed");
  }
  totalEx.awaitTermination(5, TimeUnit.SECONDS);
}
 
Example 4
Source File: TableInfo.java    From hbase-tools with Apache License 2.0 6 votes vote down vote up
private void clean(NavigableMap<HRegionInfo, ServerName> regionServerMap) throws InterruptedException, IOException {
    long timestamp = System.currentTimeMillis();

    Set<String> tableNameSet = tableNameSet(regionServerMap);

    ExecutorService executorService = Executors.newFixedThreadPool(RegionLocationCleaner.THREAD_POOL_SIZE);
    try {
        for (String tableName : tableNameSet)
            executorService.execute(
                    new RegionLocationCleaner(tableName, admin.getConfiguration(), regionServerMap));
    } finally {
        executorService.shutdown();
        executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
    }

    Util.printVerboseMessage(args, "TableInfo.clean", timestamp);
}
 
Example 5
Source File: FloRunner.java    From flo with Apache License 2.0 6 votes vote down vote up
private static Closeable executorCloser(ExecutorService executorService) {
  return () -> {
    executorService.shutdown();

    boolean terminated;
    try {
      terminated = executorService.awaitTermination(10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      terminated = false;
    }

    if (!terminated) {
      executorService.shutdownNow();
    }
  };
}
 
Example 6
Source File: LotsOfCancels.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        // create a bunch of directories. Create two tasks for each directory,
        // one to bash on cancel, the other to poll the events
        ExecutorService pool = Executors.newCachedThreadPool();
        try {
            Path top = Files.createTempDirectory("work");
            top.toFile().deleteOnExit();
            for (int i=1; i<=16; i++) {
                Path dir = Files.createDirectory(top.resolve("dir-" + i));
                WatchService watcher = FileSystems.getDefault().newWatchService();
                pool.submit(() -> handle(dir, watcher));
                pool.submit(() -> poll(watcher));
            }
        } finally {
            pool.shutdown();
        }

        // give thread pool lots of time to terminate
        if (!pool.awaitTermination(5L, TimeUnit.MINUTES))
            throw new RuntimeException("Thread pool did not terminate");

        if (failed)
            throw new RuntimeException("Test failed, see log for details");
    }
 
Example 7
Source File: ExecutorUtil.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public static void shutdownNow(Executor executor, final int timeout) {
    if (!(executor instanceof ExecutorService) || isShutdown(executor)) {
        return;
    }
    final ExecutorService es = (ExecutorService) executor;
    try {
        es.shutdownNow();
    } catch (SecurityException | NullPointerException ex2) {
        return ;
    }
    try {
        es.awaitTermination(timeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
    if (!isShutdown(es)){
        newThreadToCloseExecutor(es);
    }
}
 
Example 8
Source File: GetObjectName.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    int tasks = 10000;
    ExecutorService executor = Executors.newFixedThreadPool(10);
    submitTasks(executor, tasks);
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
    if (!failed) {
        System.out.println("Test passed.");
    }
}
 
Example 9
Source File: Async.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Shutdown as per {@link ExecutorService} Javadoc recommendation.
 *
 * @param executorService executor service we wish to shut down.
 */
private static void shutdown(ExecutorService executorService) {
    executorService.shutdown();
    try {
        if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
            executorService.shutdownNow();
            if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
                System.err.println("Thread pool did not terminate");
            }
        }
    } catch (InterruptedException ie) {
        executorService.shutdownNow();
        Thread.currentThread().interrupt();
    }
}
 
Example 10
Source File: GetObjectName.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    int tasks = 10000;
    ExecutorService executor = Executors.newFixedThreadPool(10);
    submitTasks(executor, tasks);
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
    if (!failed) {
        System.out.println("Test passed.");
    }
}
 
Example 11
Source File: BasicTasksTest.java    From streams with Apache License 2.0 5 votes vote down vote up
@Test
public void testMergeTask() {
  int numMessages = 100;
  int incoming = 5;
  StreamsMergeTask task = new StreamsMergeTask();
  BlockingQueue<StreamsDatum> outQueue = new LinkedBlockingQueue<>();
  task.addOutputQueue(outQueue);
  for(int i=0; i < incoming; ++i) {
    task.addInputQueue(createInputQueue(numMessages));
  }
  ExecutorService service = Executors.newFixedThreadPool(1);
  service.submit(task);
  int attempts = 0;
  while(outQueue.size() != incoming * numMessages ) {
    Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
    ++attempts;
    if(attempts == 10) {
      assertEquals("Processor task failed to output " + (numMessages * incoming) + " in a timely fashion.", (numMessages * incoming), outQueue.size());
    }
  }
  task.stopTask();
  service.shutdown();
  try {
    if(!service.awaitTermination(5, TimeUnit.SECONDS)){
      service.shutdownNow();
      fail("Service did not terminate.");
    }
    assertTrue("Task should have completed running in allotted time.", service.isTerminated());
  } catch (InterruptedException e) {
    fail("Test Interrupted.");
  }
}
 
Example 12
Source File: BackgroundInitializerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Tests the get() method if waiting for the initialization is interrupted.
 */
@Test
public void testGetInterruptedException() throws Exception {
    ExecutorService exec = Executors.newSingleThreadExecutor();
    final BackgroundInitializerTestImpl init = new BackgroundInitializerTestImpl(
            exec);
    final CountDownLatch latch1 = new CountDownLatch(1);
    init.shouldSleep = true;
    init.start();
    final AtomicReference<InterruptedException> iex = new AtomicReference<InterruptedException>();
    Thread getThread = new Thread() {
        @Override
        public void run() {
            try {
                init.get();
            } catch (ConcurrentException cex) {
                if (cex.getCause() instanceof InterruptedException) {
                    iex.set((InterruptedException) cex.getCause());
                }
            } finally {
                assertTrue("Thread not interrupted", isInterrupted());
                latch1.countDown();
            }
        }
    };
    getThread.start();
    getThread.interrupt();
    latch1.await();
    exec.shutdownNow();
    exec.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    assertNotNull("No interrupted exception", iex.get());
}
 
Example 13
Source File: Synchronization.java    From Learn-Java-12-Programming with MIT License 5 votes vote down vote up
private static void shutdownAndTerminate(ExecutorService pool){
    try {
        long timeout = 100;
        TimeUnit timeUnit = TimeUnit.MILLISECONDS;
        System.out.println("Waiting all threads completion for "
                + timeout + " " + timeUnit + "...");
        // Blocks until timeout or all threads complete execution,
        // or the current thread is interrupted, whichever happens first.
        boolean isTerminated =
                pool.awaitTermination(timeout, timeUnit);
        System.out.println("isTerminated()=" + isTerminated);
        if (!isTerminated) {
            System.out.println("Calling shutdownNow()...");
            List<Runnable> list = pool.shutdownNow();
            System.out.println(list.size() + " threads running");
            isTerminated =
                    pool.awaitTermination(timeout, timeUnit);
            if (!isTerminated) {
                System.out.println("Some threads are still running");
            }
            System.out.println("Exiting");
        }
    } catch (InterruptedException ex) {
        ex.printStackTrace();
    }

}
 
Example 14
Source File: MultiThreadTest.java    From java-master with Apache License 2.0 5 votes vote down vote up
@Test
public void test5() throws Exception {
    ExecutorService executorService = Executors.newCachedThreadPool();
    for (int i = 0; i < 10; i++) {
        executorService.submit(new Task4());
    }
    executorService.awaitTermination(5, TimeUnit.SECONDS);
}
 
Example 15
Source File: MultiThreadTester.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    CacheChannel cache = J2Cache.getChannel();

    String region = "Users";
    String key = "ld";

    ExecutorService threadPool = Executors.newCachedThreadPool();

    cache.evict(region, key);
    //cache.set(region, key, "Winter Lau");

    for(int i=0;i<100;i++) {
        final int seq = i;
        threadPool.execute(() -> {
            String name = "Thread-" + seq;
            for(int j=0;j<1;j++) {
                long ct = System.currentTimeMillis();
                System.out.printf("%s -> %s (%dms)\n", name, cache.get(region, key), (System.currentTimeMillis()-ct));
            }
        });
    }

    threadPool.shutdown();
    threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);

    System.exit(0);
}
 
Example 16
Source File: ScopeRequestIntegrationTest.java    From dagger-servlet with Apache License 2.0 5 votes vote down vote up
@Test
public final void testNonHttpRequestScopedCallable()
        throws ServletException, IOException, InterruptedException, ExecutionException {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    ObjectGraph baseGraph = ObjectGraph.create(TestAppModule.class);
    ObjectGraph scopingGraph = ScopingObjectGraph.create(baseGraph)
            .addScopedModules(RequestScoped.class, TestRequestModule.class);
    ObjectGraph scopedGraph = baseGraph.plus(TestRequestModule.class);

    scopingGraph.get(InternalServletModule.ObjectGraphProvider.class).set(scopingGraph);

    SomeObject someObject = new SomeObject(A_VALUE);
    OffRequestCallable offRequestCallable = scopingGraph.get(OffRequestCallable.class);
    executor.submit(ServletScopes.scopeRequest(offRequestCallable,
            ImmutableMap.<Class<?>, Object>of(SomeObject.class, someObject,
                    ObjectGraph.class, scopedGraph))).get();

    assertSame(scopingGraph.get(OffRequestCallable.class), offRequestCallable);

    // Make sure the value was passed on.
    assertEquals(someObject.value, offRequestCallable.value);
    assertFalse(SHOULDNEVERBESEEN.equals(someObject.value));

    // Now create a new request and assert that the scopes don't cross.
    someObject = new SomeObject(A_DIFFERENT_VALUE);
    executor.submit(ServletScopes.scopeRequest(offRequestCallable,
            ImmutableMap.<Class<?>, Object>of(SomeObject.class, someObject))).get();

    assertSame(scopingGraph.get(OffRequestCallable.class), offRequestCallable);

    // Make sure the value was passed on.
    assertEquals(someObject.value, offRequestCallable.value);
    assertFalse(SHOULDNEVERBESEEN.equals(someObject.value));
    executor.shutdown();
    executor.awaitTermination(2, TimeUnit.SECONDS);
}
 
Example 17
Source File: ContentReviewServiceTurnitinOC.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public void processQueue() {
	log.info("Processing Turnitin OC submission queue");
	// Create new session object to ensure permissions are carried correctly to each new thread
	final Session session = sessionManager.getCurrentSession();
	ExecutorService executor = Executors.newFixedThreadPool(2);
	executor.execute(new Runnable() {
		@Override
		public void run() {
			sessionManager.setCurrentSession(session);
			processUnsubmitted();
		}
	});
	executor.execute(new Runnable() {
		@Override
		public void run() {
			sessionManager.setCurrentSession(session);
			checkForReport();
		}
	});
	executor.shutdown();
	// wait:
	try {
		if(!executor.awaitTermination(30, TimeUnit.MINUTES)){
			log.error("ContentReviewServiceTurnitinOC.processQueue: time out waiting for executor to complete");
		}
	} catch (InterruptedException e) {
		log.error(e.getMessage(), e);
	}
}
 
Example 18
Source File: QuerySpanTest.java    From datawave with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiThreadedQuerySpanAcrossThreads() {
    
    MultiThreadedQuerySpan qs1 = new MultiThreadedQuerySpan(null);
    advanceIterators(qs1);
    MultiThreadedQuerySpan qs2 = new MultiThreadedQuerySpan(null);
    advanceIterators(qs2);
    MultiThreadedQuerySpan qs3 = new MultiThreadedQuerySpan(null);
    advanceIterators(qs3);
    
    QuerySpanCollector qsc = new QuerySpanCollector();
    
    Runnable r1 = new QSRunnable(qsc, qs1);
    Runnable r2 = new QSRunnable(qsc, qs2);
    Runnable r3 = new QSRunnable(qsc, qs3);
    
    ExecutorService executorService = new SimpleThreadPool(10, "QSExecutor");
    executorService.execute(r1);
    executorService.execute(r2);
    executorService.execute(r3);
    executorService.shutdown();
    try {
        executorService.awaitTermination(10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    
    QuerySpan qs4 = qsc.getCombinedQuerySpan(null);
    
    Assert.assertEquals(21, qs4.getSeekCount());
    Assert.assertEquals(48, qs4.getNextCount());
    Assert.assertTrue(qs4.getYield());
    Assert.assertEquals(12, qs4.getSourceCount());
}
 
Example 19
Source File: CurrentThreadExecutorServiceTest.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void awaitTerminationWithoutShutdown() throws InterruptedException {
    ExecutorService executorService = new CurrentThreadExecutorService();
    expected.expect(IllegalStateException.class);
    executorService.awaitTermination(1, TimeUnit.SECONDS);
}
 
Example 20
Source File: Util.java    From glowroot with Apache License 2.0 4 votes vote down vote up
private static void runTest(String modulePath, @Nullable String test, JavaVersion javaVersion,
        String... profiles) throws Exception {
    System.out.println(javaVersion);
    report.println(javaVersion);
    List<String> command = Lists.newArrayList();
    command.add(MVN);
    command.add("-P");
    // don't recompile plugin classes since in some cases they won't recompile with older
    // versions of libraries
    // need to recompile tests in some cases to align bytecode with test version
    // also need to recompile tests in some cases where default test compilation target is 1.7
    // but there is alternate 1.6 profile triggered above by -Dglowroot.test.java6
    StringBuilder sb = new StringBuilder("compile-test-classes-only,fail-failsafe-if-no-tests");
    List<String> propertyArgs = Lists.newArrayList();
    for (String profile : profiles) {
        if (profile.startsWith("-D")) {
            propertyArgs.add(profile);
        } else {
            sb.append(",");
            sb.append(profile);
        }
    }
    command.add(sb.toString());
    for (String propertyArg : propertyArgs) {
        command.add(propertyArg);
    }
    command.add("-pl");
    command.add(modulePath);
    command.add("-Djvm=" + javaVersion.getJavaHome() + File.separator + "bin" + File.separator
            + "java");
    if (javaVersion == JavaVersion.JAVA6 || javaVersion == JavaVersion.JAVA7) {
        // maven central no longer supports TLSv1.1 and below
        command.add("-Dhttps.protocols=TLSv1.2");
    }
    if (javaVersion == JavaVersion.JAVA6) {
        command.add("-Dglowroot.test.java6");
    }
    // cassandra plugin tests need java7.home when running under java 6 in order to run
    // cassandra itself
    command.add("-Djava7.home=" + JAVA7.getJavaHome());
    if (test != null) {
        command.add("-Dit.test=" + test);
    }
    command.add("-Dglowroot.it.harness=javaagent");
    command.add("-Dglowroot.test.shaded");
    command.add("-Denforcer.skip");
    command.add("-Danimal.sniffer.skip");
    String sourceOfRandomness = System.getProperty("java.security.egd");
    if (sourceOfRandomness != null) {
        command.add("-Djava.security.egd=" + sourceOfRandomness);
    }
    command.add("clean"); // using profile above "clean-test-classes-only"
    command.add("verify");
    ProcessBuilder processBuilder = new ProcessBuilder(command);
    processBuilder.redirectErrorStream(true);
    processBuilder.directory(new File(BASE_DIR));
    if (javaVersion == JavaVersion.JAVA6) {
        // maven requires Java 7+
        processBuilder.environment().put("JAVA_HOME", JavaVersion.JAVA7.getJavaHome());
    } else {
        processBuilder.environment().put("JAVA_HOME", javaVersion.getJavaHome());
    }
    System.out.println("\n\n" + Joiner.on(' ').join(command) + "\n\n");
    Process process = processBuilder.start();
    InputStream in = checkNotNull(process.getInputStream());
    ConsoleOutputPipe consoleOutputPipe = new ConsoleOutputPipe(in, System.out);
    ExecutorService consolePipeExecutorService = Executors.newSingleThreadExecutor();
    Future<?> future = consolePipeExecutorService.submit(consoleOutputPipe);
    int exit = process.waitFor();
    future.get();
    consolePipeExecutorService.shutdown();
    consolePipeExecutorService.awaitTermination(10, SECONDS);
    if (exit != 0) {
        report.println("FAIL");
    }
    System.out.println("\n\n");
}