下面列出了com.google.common.util.concurrent.MoreExecutors#shutdownAndAwaitTermination ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@After
public void tearDown() {
if (server != null) {
server.shutdown();
}
if (channel != null) {
channel.shutdown();
}
MoreExecutors.shutdownAndAwaitTermination(executor, 5, TimeUnit.SECONDS);
}
@Override
public void destroy() {
repository.close();
repositoryContext.getEventBus().unregister(this);
if (listeningExecutorService != null) {
logger.log(Level.INFO, "Shutting down the executor service.");
MoreExecutors.shutdownAndAwaitTermination(listeningExecutorService, 5L, TimeUnit.MINUTES);
}
}
/** Releases resources and shuts down worker processes. */
@Override
public void destroy() {
identityRepository.close();
try {
stateManager.close();
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to close identity state sucessfully", e);
}
if (listeningExecutorService != null) {
MoreExecutors.shutdownAndAwaitTermination(listeningExecutorService, 5, TimeUnit.MINUTES);
}
}
public static void getInParallel(OkHttpClient client, String url, int count) {
ExecutorService exec = Executors.newFixedThreadPool(count);
for (int i = 0; i < count; i++) {
exec.submit(() -> Http.get(client, url));
}
MoreExecutors.shutdownAndAwaitTermination(exec, 30, TimeUnit.SECONDS);
}
@Override
public void stop() {
if (mailSenderExecutorService.isPresent()) {
MoreExecutors.shutdownAndAwaitTermination(
mailSenderExecutorService.get(),
1,
TimeUnit.SECONDS
);
}
}
@After
public void tearDown() {
if (server != null) {
server.shutdown();
}
if (channel != null) {
channel.shutdown();
}
MoreExecutors.shutdownAndAwaitTermination(executor, 5, TimeUnit.SECONDS);
}
public static void getInParallel(OkHttpClient client, String url, int count) {
ExecutorService exec = Executors.newFixedThreadPool(count);
for (int i = 0; i < count; i++) {
exec.submit(() -> Http.get(client, url));
}
MoreExecutors.shutdownAndAwaitTermination(exec, 30, TimeUnit.SECONDS);
}
public void shutdown() {
MoreExecutors.shutdownAndAwaitTermination(processingPool, 1, TimeUnit.MINUTES);
//MoreExecutors.shutdownAndAwaitTermination(databaseExecutor, 1, TimeUnit.MINUTES);
}
public static void shutdown() {
MoreExecutors.shutdownAndAwaitTermination(service, 60, TimeUnit.SECONDS);
}
@After
public final void shutdownExecutor() throws Exception {
MoreExecutors.shutdownAndAwaitTermination(executorService, TestUtils.WAIT_TIMEOUT_SECONDS,
TimeUnit.SECONDS);
}
@AfterClass
public static void tearDownExecutor() {
MoreExecutors.shutdownAndAwaitTermination(executorService, 5, TimeUnit.SECONDS);
}
@Override
public void close() throws IOException {
MoreExecutors.shutdownAndAwaitTermination(callbackExecutor, 5, TimeUnit.MINUTES);
isRunning.set(false);
}
/**
* @see gracefulShutdown
*/
public static boolean gracefulShutdown(@Nullable ExecutorService threadPool, int shutdownTimeout,
TimeUnit timeUnit) {
return threadPool == null || MoreExecutors.shutdownAndAwaitTermination(threadPool, shutdownTimeout, timeUnit);
}
/**
* Close this persister and wait for pending write operations to complete.
*/
public void close() {
MoreExecutors.shutdownAndAwaitTermination(writer, 5, TimeUnit.SECONDS);
}
/** An orderly shutdown of this executor service. */
public synchronized void shutdown() {
MoreExecutors.shutdownAndAwaitTermination(delegate, 2500, TimeUnit.MILLISECONDS);
cancelAll();
}
/**
* This method must be called when the test group execution is finished, for example, from
* @AfterClass method.
*/
public final void dispose() {
Preconditions.checkNotNull(executorService);
MoreExecutors.shutdownAndAwaitTermination(executorService, 1, TimeUnit.SECONDS);
executorService = null;
}
/**
* @see gracefulShutdown
*/
public static boolean gracefulShutdown( ExecutorService threadPool, int shutdownTimeout,
TimeUnit timeUnit) {
return threadPool == null || MoreExecutors.shutdownAndAwaitTermination(threadPool, shutdownTimeout, timeUnit);
}
@VisibleForTesting
static void buildMergedDexFiles(Options options) throws IOException {
ListeningExecutorService executor;
checkArgument(!options.inputArchives.isEmpty(), "Need at least one --input");
checkArgument(
options.mainDexListFile == null || options.inputArchives.size() == 1,
"--main-dex-list only supported with exactly one --input, use DexFileSplitter for more");
if (options.multidexMode.isMultidexAllowed()) {
executor = createThreadPool();
} else {
checkArgument(
options.mainDexListFile == null,
"--main-dex-list is only supported with multidex enabled, but mode is: %s",
options.multidexMode);
checkArgument(
!options.minimalMainDex,
"--minimal-main-dex is only supported with multidex enabled, but mode is: %s",
options.multidexMode);
// We'll only ever merge and write one dex file, so multi-threading is pointless.
executor = MoreExecutors.newDirectExecutorService();
}
ImmutableSet<String> classesInMainDex = options.mainDexListFile != null
? ImmutableSet.copyOf(Files.readAllLines(options.mainDexListFile, UTF_8))
: null;
PrintStream originalStdOut = System.out;
try (DexFileAggregator out = createDexFileAggregator(options, executor)) {
if (!options.verbose) {
// com.android.dx.merge.DexMerger prints status information to System.out that we silence
// here unless it was explicitly requested. (It also prints debug info to DxContext.out,
// which we populate accordingly below.)
System.setOut(Dexing.nullout);
}
LinkedHashSet<String> seen = new LinkedHashSet<>();
for (Path inputArchive : options.inputArchives) {
// Simply merge files from inputs in order. Doing that with a main dex list doesn't work,
// but we rule out more than one input with a main dex list above.
try (ZipFile zip = new ZipFile(inputArchive.toFile())) {
ArrayList<ZipEntry> dexFiles = filesToProcess(zip);
if (classesInMainDex == null) {
processDexFiles(zip, dexFiles, seen, out);
} else {
// To honor --main_dex_list make two passes:
// 1. process only the classes listed in the given file
// 2. process the remaining files
Predicate<ZipEntry> mainDexFilter =
ZipEntryPredicates.classFileFilter(classesInMainDex);
processDexFiles(zip, Iterables.filter(dexFiles, mainDexFilter), seen, out);
// Fail if main_dex_list is too big, following dx's example
checkState(out.getDexFilesWritten() == 0, "Too many classes listed in main dex list "
+ "file %s, main dex capacity exceeded", options.mainDexListFile);
if (options.minimalMainDex) {
out.flush(); // Start new .dex file if requested
}
processDexFiles(
zip, Iterables.filter(dexFiles, Predicates.not(mainDexFilter)), seen, out);
}
}
}
} finally {
// Kill threads in the pool so we don't hang
MoreExecutors.shutdownAndAwaitTermination(executor, 1, SECONDS);
System.setOut(originalStdOut);
}
}
@AfterClass
public void afterClass() {
MoreExecutors.shutdownAndAwaitTermination(executor, 1, TimeUnit.MINUTES);
}
/**
* 按照ExecutorService JavaDoc示例代码编写的Graceful Shutdown方法.
*
* 先使用shutdown, 停止接收新任务并尝试完成所有已存在任务.
*
* 如果1/2超时时间后, 则调用shutdownNow,取消在workQueue中Pending的任务,并中断所有阻塞函数.
*
* 如果1/2超时仍然超時,則強制退出.
*
* 另对在shutdown时线程本身被调用中断做了处理.
*
* 返回线程最后是否被中断.
*
* 使用了Guava的工具类
* @see MoreExecutors#shutdownAndAwaitTermination(ExecutorService, long, TimeUnit)
*/
public static boolean gracefulShutdown(@Nullable ExecutorService threadPool, int shutdownTimeoutMills) {
return threadPool == null
|| MoreExecutors.shutdownAndAwaitTermination(threadPool, shutdownTimeoutMills, TimeUnit.MILLISECONDS);
}