java.util.concurrent.ExecutorService.isTerminated()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(366)

本文整理了Java中java.util.concurrent.ExecutorService.isTerminated()方法的一些代码示例,展示了ExecutorService.isTerminated()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ExecutorService.isTerminated()方法的具体详情如下:
包路径:java.util.concurrent.ExecutorService
类名称:ExecutorService
方法名:isTerminated

ExecutorService.isTerminated介绍

[英]Returns true if all tasks have completed following shut down. Note that isTerminated is never true unless either shutdown or shutdownNow was called first.
[中]如果关闭后所有任务都已完成,则返回true。请注意,除非先调用shutdown或shutdownNow,否则isTerminated永远不会为true。

代码示例

代码示例来源:origin: iluwatar/java-design-patterns

/**
 * Stops logging clients. This is a blocking call.
 */
public void stop() {
 service.shutdown();
 if (!service.isTerminated()) {
  service.shutdownNow();
  try {
   service.awaitTermination(1000, TimeUnit.SECONDS);
  } catch (InterruptedException e) {
   LOGGER.error("exception awaiting termination", e);
  }
 }
 LOGGER.info("Logging clients stopped");
}

代码示例来源:origin: alibaba/fescar

@Override
public void shutdown() {
  if (null != fileWriteExecutor) {
    fileWriteExecutor.shutdown();
    stopping = true;
    int retry = 0;
    while (!fileWriteExecutor.isTerminated() && retry < MAX_SHUTDOWN_RETRY) {
      ++retry;
      try {
        Thread.sleep(SHUTDOWN_CHECK_INTERNAL);
      } catch (InterruptedException exx) {}
    }
    if (retry >= MAX_SHUTDOWN_RETRY) {
      fileWriteExecutor.shutdownNow();
    }
  }
}

代码示例来源:origin: kaaproject/kaa

/**
 * Bean destroy-method. Call before bean destroying.
 */
public void destroy() {
 if (sendPool != null) {
  sendPool.shutdown();
  try {
   while (sendPool.isTerminated() == false) {
    sendPool.awaitTermination(sendTimeout, TimeUnit.SECONDS);
   }
  } catch (InterruptedException ex) {
   LOG.warn("shutdown interrupted on {}", sendPool, ex);
  }
 }
}

代码示例来源:origin: apache/activemq

/**
 * Shutdown now the given executor service aggressively.
 *
 * @param executorService the executor service to shutdown now
 * @return list of tasks that never commenced execution
 * @see java.util.concurrent.ExecutorService#shutdownNow()
 */
public static List<Runnable> shutdownNow(ExecutorService executorService) {
  List<Runnable> answer = null;
  if (!executorService.isShutdown()) {
    LOG.debug("Forcing shutdown of ExecutorService: {}", executorService);
    answer = executorService.shutdownNow();
    if (LOG.isTraceEnabled()) {
      LOG.trace("Shutdown of ExecutorService: {} is shutdown: {} and terminated: {}.",
          new Object[]{executorService, executorService.isShutdown(), executorService.isTerminated()});
    }
  }
  return answer;
}

代码示例来源:origin: stackoverflow.com

final ExecutorService pool = Executors.newFixedThreadPool(2);
final CompletionService<String> service = new ExecutorCompletionService<String>(pool);
final List<? extends Callable<String>> callables = Arrays.asList(
  new SleepingCallable("slow", 5000),
  new SleepingCallable("quick", 500));
for (final Callable<String> callable : callables) {
 service.submit(callable);
}
pool.shutdown();
try {
 while (!pool.isTerminated()) {
  final Future<String> future = service.take();
  System.out.println(future.get());
 }
} catch (ExecutionException | InterruptedException ex) { }

代码示例来源:origin: alibaba/canal

@Override
public void stop() {
  // fix bug #968,对于pool与
  workerPool.halt();
  simpleParserStage.halt();
  sinkStoreStage.halt();
  try {
    parserExecutor.shutdownNow();
    while (!parserExecutor.awaitTermination(1, TimeUnit.SECONDS)) {
      if (parserExecutor.isShutdown() || parserExecutor.isTerminated()) {
        break;
      }
      parserExecutor.shutdownNow();
    }
  } catch (Throwable e) {
    // ignore
  }
  try {
    stageExecutor.shutdownNow();
    while (!stageExecutor.awaitTermination(1, TimeUnit.SECONDS)) {
      if (stageExecutor.isShutdown() || stageExecutor.isTerminated()) {
        break;
      }
      stageExecutor.shutdownNow();
    }
  } catch (Throwable e) {
    // ignore
  }
  super.stop();
}

代码示例来源:origin: sarxos/webcam-capture

public void shutdown() {
  if (started.compareAndSet(true, false)) {
    LOG.debug("Shutting down webcam processor");
    runner.shutdown();
    LOG.debug("Awaiting tasks termination");
    while (runner.isTerminated()) {
      try {
        runner.awaitTermination(100, TimeUnit.MILLISECONDS);
      } catch (InterruptedException e) {
        return;
      }
      runner.shutdownNow();
    }
    LOG.debug("All tasks has been terminated");
  }
}

代码示例来源:origin: linkedin/cruise-control

public void shutdown() {
 LOG.info("Shutting down goal optimizer.");
 _shutdown = true;
 _proposalPrecomputingExecutor.shutdown();
 try {
  _proposalPrecomputingExecutor.awaitTermination(30000L, TimeUnit.MILLISECONDS);
  if (!_proposalPrecomputingExecutor.isTerminated()) {
   LOG.warn("The goal optimizer failed to shutdown in 30000 ms.");
  }
 } catch (InterruptedException e) {
  LOG.warn("Interrupted while waiting for goal optimizer to shutdown.");
 }
 LOG.info("Goal optimizer shutdown completed.");
}

代码示例来源:origin: stackoverflow.com

final Runnable stuffToDo = new Thread() {
 @Override 
 public void run() { 
  /* Do stuff here. */ 
 }
};

final ExecutorService executor = Executors.newSingleThreadExecutor();
final Future future = executor.submit(stuffToDo);
executor.shutdown(); // This does not cancel the already-scheduled task.

try { 
 future.get(5, TimeUnit.MINUTES); 
}
catch (InterruptedException ie) { 
 /* Handle the interruption. Or ignore it. */ 
}
catch (ExecutionException ee) { 
 /* Handle the error. Or ignore it. */ 
}
catch (TimeoutException te) { 
 /* Handle the timeout. Or ignore it. */ 
}
if (!executor.isTerminated())
  executor.shutdownNow(); // If you want to stop the code that hasn't finished.

代码示例来源:origin: apache/incubator-pinot

if (!executor.isTerminated()) {
 executor.shutdownNow();

代码示例来源:origin: apache/hbase

/**
 * Run the application's maps using a thread pool.
 */
@Override
public void run(Context context) throws IOException, InterruptedException {
 outer = context;
 int numberOfThreads = getNumberOfThreads(context);
 mapClass = getMapperClass(context);
 if (LOG.isDebugEnabled()) {
  LOG.debug("Configuring multithread runner to use " + numberOfThreads +
    " threads");
 }
 executor = Executors.newFixedThreadPool(numberOfThreads);
 for(int i=0; i < numberOfThreads; ++i) {
  MapRunner thread = new MapRunner(context);
  executor.execute(thread);
 }
 executor.shutdown();
 while (!executor.isTerminated()) {
  // wait till all the threads are done
  Thread.sleep(1000);
 }
}

代码示例来源:origin: pulsarIO/realtime-analytics

task.running = false;
pool.shutdownNow();
  pool.awaitTermination(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
  LOGGER.error(e.getMessage(), e);
if (!pool.isTerminated()) {
  LOGGER.error("Timed out when shutdown sessionizer thread pool");
  if (timer.isTerminated() && pool.isTerminated()) {
    break;

代码示例来源:origin: google/guava

long halfTimeoutNanos = unit.toNanos(timeout) / 2;
service.shutdown();
try {
 if (!service.awaitTermination(halfTimeoutNanos, TimeUnit.NANOSECONDS)) {
  service.shutdownNow();
  service.awaitTermination(halfTimeoutNanos, TimeUnit.NANOSECONDS);
 service.shutdownNow();
return service.isTerminated();

代码示例来源:origin: apache/geode

void stopExecutor(ExecutorService executor) {
 if (executor == null) {
  return;
 }
 executor.shutdown();
 final int secToWait = Integer
   .getInteger(DistributionConfig.GEMFIRE_PREFIX + "prrecovery-close-timeout", 120).intValue();
 try {
  executor.awaitTermination(secToWait, TimeUnit.SECONDS);
 } catch (InterruptedException x) {
  Thread.currentThread().interrupt();
  logger.debug("Failed in interrupting the Resource Manager Thread due to interrupt");
 }
 if (!executor.isTerminated()) {
  logger.warn("Failed to stop resource manager threads in {} seconds",
    secToWait);
 }
}

代码示例来源:origin: pentaho/pentaho-kettle

private void closeSessionMonitor() {
 if ( sessionMonitor != null && !sessionMonitor.isShutdown() ) {
  try {
   getLogChannel().logDebug( "Shutting down the Session Monitor." );
   sessionMonitor.shutdown();
  } finally {
   if ( !sessionMonitor.isTerminated() ) {
    sessionMonitor.shutdownNow();
   }
   getLogChannel().logDebug( "Session Monitor shutdown." );
  }
 }
}

代码示例来源:origin: kaaproject/kaa

if (executorService != null && !executorService.isTerminated()) {
 for (TSocketWrapper socket : new ArrayList<>(openedSockets)) {
  if (socket.getSocket() != null && !socket.getSocket().isClosed()) {
 executorService.shutdownNow();

代码示例来源:origin: neo4j/neo4j

@After
public void cleanup()
{
  if ( executorService != null && !executorService.isTerminated() )
  {
    executorService.shutdown();
  }
}

代码示例来源:origin: helun/Ektorp

private void afterLoad(ExecutorService es) throws InterruptedException {
  es.awaitTermination(5, TimeUnit.MINUTES);
  if (es.isTerminated()) {
    for (DataLoader l : loaders) {
      l.allDataLoaded();
    }    
  } else {
    log.error("The following data loaders did not complete in time: ");
    for(Runnable r : es.shutdownNow()) {
      log.error("The following data loaders did not complete in time: {}", r);	
    }
  }
}

代码示例来源:origin: andsel/moquette

/**
 * Shutdown graciously the executor service
 */
void stop() {
  LOG.info("Shutting down interceptor thread pool...");
  executor.shutdown();
  try {
    LOG.info("Waiting for thread pool tasks to terminate...");
    executor.awaitTermination(10L, TimeUnit.SECONDS);
  } catch (InterruptedException e) {
  }
  if (!executor.isTerminated()) {
    LOG.warn("Forcing shutdown of interceptor thread pool...");
    executor.shutdownNow();
  }
  LOG.info("interceptors stopped");
}

代码示例来源:origin: google/guava

public void testShutdownAndAwaitTermination_immediateShutdownInternal() throws Exception {
 ExecutorService service = mock(ExecutorService.class);
 when(service.awaitTermination(HALF_SECOND_NANOS, NANOSECONDS)).thenReturn(true);
 when(service.isTerminated()).thenReturn(true);
 assertTrue(shutdownAndAwaitTermination(service, 1L, SECONDS));
 verify(service).shutdown();
 verify(service).awaitTermination(HALF_SECOND_NANOS, NANOSECONDS);
}

相关文章