本文整理汇总了Java中com.google.common.util.concurrent.ListeningExecutorService.shutdownNow方法的典型用法代码示例。如果您正苦于以下问题:Java ListeningExecutorService.shutdownNow方法的具体用法?Java ListeningExecutorService.shutdownNow怎么用?Java ListeningExecutorService.shutdownNow使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.util.concurrent.ListeningExecutorService
的用法示例。
在下文中一共展示了ListeningExecutorService.shutdownNow方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getS3Logs
import com.google.common.util.concurrent.ListeningExecutorService; //导入方法依赖的package包/类
private List<SingularityS3Log> getS3Logs(S3Configuration s3Configuration, Optional<String> group, Collection<String> prefixes) throws InterruptedException, ExecutionException, TimeoutException {
if (prefixes.isEmpty()) {
return Collections.emptyList();
}
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(Math.min(prefixes.size(), s3Configuration.getMaxS3Threads()),
new ThreadFactoryBuilder().setNameFormat("S3LogFetcher-%d").build()));
try {
List<SingularityS3Log> logs = Lists.newArrayList(getS3LogsWithExecutorService(s3Configuration, group, executorService, prefixes));
Collections.sort(logs, LOG_COMPARATOR);
return logs;
} finally {
executorService.shutdownNow();
}
}
示例2: onFinish
import com.google.common.util.concurrent.ListeningExecutorService; //导入方法依赖的package包/类
private void onFinish(SingularityExecutorTask task, Protos.TaskState taskState) {
processKiller.cancelDestroyFuture(task.getTaskId());
tasks.remove(task.getTaskId());
processRunningTasks.remove(task.getTaskId());
processBuildingTasks.remove(task.getTaskId());
task.cleanup(taskState);
ListeningExecutorService executorService = taskToShellCommandPool.remove(task.getTaskId());
if (executorService != null) {
executorService.shutdownNow();
try {
executorService.awaitTermination(5, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
LOG.warn("Awaiting shutdown of shell executor service", e);
}
}
logging.stopTaskLogger(task.getTaskId(), task.getLogbackLog());
checkIdleExecutorShutdown(task.getDriver());
}
示例3: shutdown
import com.google.common.util.concurrent.ListeningExecutorService; //导入方法依赖的package包/类
private void shutdown(@Nullable final ListeningExecutorService executorService) {
if (executorService != null) {
executorService.shutdown();
try {
// Wait a while for existing tasks to terminate
if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
executorService.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
log.warn("Thread pool for metacat refresh did not terminate");
}
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
executorService.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
}
示例4: exceptionArentCached_deferredFuture
import com.google.common.util.concurrent.ListeningExecutorService; //导入方法依赖的package包/类
@Test
public void exceptionArentCached_deferredFuture() throws Exception {
ListeningExecutorService exec = listeningDecorator(Executors.newSingleThreadExecutor());
try {
executor = TestDeduplicatingExecutor.create(s -> {
if (s == first) {
return exec.submit(() -> {
Thread.sleep(50);
throw new IllegalArgumentException();
});
}
return Futures.immediateFuture(s);
});
exceptionsArentCached();
} finally {
exec.shutdownNow();
}
}
示例5: invokeAllAndShutdownWhenFinish
import com.google.common.util.concurrent.ListeningExecutorService; //导入方法依赖的package包/类
public static <V> List<V> invokeAllAndShutdownWhenFinish(List<Callable<V>> tasks, ListeningExecutorService executor, FutureCallback<V>[] callbacks)
{
final List<V> results = Lists.newArrayList();
final CountDownLatch endSignal = new CountDownLatch(tasks.size());
FutureCallback<V> endSignalCallback = new FutureCallback<V>()
{
@Override
public void onSuccess(@Nullable V result)
{
endSignal.countDown();
results.add(result);
}
@Override
public void onFailure(Throwable t)
{
endSignal.countDown();
}
};
List<FutureCallback<V>> l = Lists.newArrayList(callbacks);
l.add(endSignalCallback);
invokeAll(tasks, executor, l.toArray(new FutureCallback[l.size()]));
try
{
endSignal.await();
executor.shutdownNow();
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
return results;
}
示例6: testConcurrentImportTrustCertificate
import com.google.common.util.concurrent.ListeningExecutorService; //导入方法依赖的package包/类
/**
* Confirm concurrent {@link KeyStoreManagerImpl#importTrustCertificate(Certificate, String)} invocations
* occur safely.
*/
@Test
public void testConcurrentImportTrustCertificate() throws Exception {
X509Certificate certificate1 = generateCertificate(10,
"concurrency-1", "ou", "o", "l", "st", "country");
X509Certificate certificate2 = generateCertificate(10,
"concurrency-2", "ou", "o", "l", "st", "country");
KeyStoreManagerConfiguration configuration = createMockConfiguration();
KeystoreInstance trustStore = mock(KeystoreInstance.class);
CountDownLatch block = new CountDownLatch(1);
// any calls to trustStore#importTrustCertificate should block on the latch
doAnswer(blockingAnswer(block))
.when(trustStore)
.importTrustCertificate(
any(Certificate.class), any(String.class),
any(char[].class)
);
KeyStoreManagerImpl manager = new KeyStoreManagerImpl(crypto, configuration, null, trustStore);
ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));
List<ListenableFuture<String>> futures = new ArrayList<>();
try {
futures.add(service.submit(() -> {
manager.importTrustCertificate(certificate1, "concurrency-1");
return "concurrency-1";
}));
futures.add(service.submit(() -> {
manager.importTrustCertificate(certificate2, "concurrency-2");
return "concurrency-2";
}));
// no matter how long we wait, this list should be empty if we've guarded correctly
List<String> results = Futures.successfulAsList(futures).get(100, TimeUnit.MILLISECONDS);
assertEquals(0, results.size());
} catch (TimeoutException e) {
// expected; from Futures.successfulAsList().get()
} finally {
// release the latch so those threads are unblocked
block.countDown();
service.shutdownNow();
}
// a passing test will show that we only called KeyStoreInstance#importTrustCertificate once and only once
// if we see more than one invocation, we passed the concurrency guard, which is unsafe
// since KeystoreInstance is not thread-safe
verify(trustStore, times(1))
.importTrustCertificate(
any(Certificate.class), any(String.class),
any(char[].class));
}
示例7: testConcurrentGenerateAndStoreKeyPair
import com.google.common.util.concurrent.ListeningExecutorService; //导入方法依赖的package包/类
/**
* Confirm concurrent {@link KeyStoreManager#generateAndStoreKeyPair(String, String, String, String, String, String)}
* invocations occur safely.
*/
@Test
public void testConcurrentGenerateAndStoreKeyPair() throws Exception {
KeyStoreManagerConfiguration configuration = createMockConfiguration();
KeystoreInstance privateStore = mock(KeystoreInstance.class);
CountDownLatch block = new CountDownLatch(1);
KeyStoreManagerImpl manager = new KeyStoreManagerImpl(crypto, configuration, privateStore, null);
// any calls to privateStore#generateKeyPair should block
doAnswer(blockingAnswer(block))
.when(privateStore)
.generateKeyPair(any(String.class), any(char[].class), any(char[].class), any(String.class),
anyInt(), any(String.class), anyInt(), any(String.class), any(String.class), any(String.class),
any(String.class), any(String.class), any(String.class));
ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));
List<ListenableFuture<String>> futures = new ArrayList<>();
try {
futures.add(service.submit(() -> {
manager.generateAndStoreKeyPair("concurrency-1",
"ou", "o", "l", "st", "c");
return "concurrency-1";
}));
futures.add(service.submit(() -> {
manager.generateAndStoreKeyPair("concurrency-2",
"ou", "o", "l", "st", "c");
return "concurrency-2";
}));
List<String> results = Futures.successfulAsList(futures).get(100, TimeUnit.MILLISECONDS);
assertEquals(0, results.size());
} catch (TimeoutException e) {
// expected; from Futures.successfulAsList().get()
} finally {
// release the latch so those threads are unblocked
block.countDown();
service.shutdownNow();
}
// a passing test will show that we only called KeyStoreInstance#generateKeyPair once and only once
// if we see more than one invocation, we passed the concurrency guard, which is unsafe
// since KeystoreInstance is not thread-safe
verify(privateStore, times(1))
.generateKeyPair(any(String.class), any(char[].class), any(char[].class), any(String.class),
anyInt(), any(String.class), anyInt(), any(String.class), any(String.class), any(String.class),
any(String.class), any(String.class), any(String.class));
}
示例8: update_HighConcurrency
import com.google.common.util.concurrent.ListeningExecutorService; //导入方法依赖的package包/类
@Test
public void update_HighConcurrency() {
final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
try {
updateHelper(new Consumer<UpdateFunction<ImmutableTreeList<Integer>>>() {
@Override
public void accept(final UpdateFunction<ImmutableTreeList<Integer>> func) {
final AtomicReference<ImmutableTreeList<Integer>> list = new AtomicReference<ImmutableTreeList<Integer>>(ImmutableTreeList.<Integer>create());
int concurrencyLevel = Runtime.getRuntime().availableProcessors();
final int iterations = 500;
List<ListenableFuture<Void>> tasks = new ArrayList<ListenableFuture<Void>>();
for (int i = 0; i < concurrencyLevel; i++) {
tasks.add(null);
}
final CountDownLatch latch = new CountDownLatch(tasks.size());
for (int i = 0; i < tasks.size(); i++) {
tasks.set(i, executor.<Void>submit(new Runnable() {
@Override
public void run() {
// Maximize concurrency by blocking this thread until all the other threads are ready to go as well.
latch.countDown();
Uninterruptibles.awaitUninterruptibly(latch);
for (int j = 0; j < iterations; j++) {
assertTrue(func.apply(
list,
new UnaryOperator<ImmutableTreeList<Integer>>() {
@Override
public ImmutableTreeList<Integer> apply(ImmutableTreeList<Integer> l) {
return l.add(l.size());
}
}));
}
}
}, null));
}
try {
Uninterruptibles.getUninterruptibly(Futures.allAsList(tasks));
} catch (ExecutionException ex) {
throw new UncheckedExecutionException(ex);
}
assertEquals(concurrencyLevel * iterations, list.get().size());
for (int i = 0; i < list.get().size(); i++) {
assertEquals(i, (int)list.get().get(i));
}
}
});
} finally {
executor.shutdownNow();
}
}
示例9: update_HighConcurrency
import com.google.common.util.concurrent.ListeningExecutorService; //导入方法依赖的package包/类
@Test
public void update_HighConcurrency() {
final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
try {
updateHelper(new Consumer<UpdateFunction<ImmutableTreeList<Integer>>>() {
@Override
public void accept(final UpdateFunction<ImmutableTreeList<Integer>> func) {
final Holder<ImmutableTreeList<Integer>> list = new Holder<ImmutableTreeList<Integer>>(ImmutableTreeList.<Integer>create());
int concurrencyLevel = Runtime.getRuntime().availableProcessors();
final int iterations = 500;
List<ListenableFuture<Void>> tasks = new ArrayList<ListenableFuture<Void>>();
for (int i = 0; i < concurrencyLevel; i++) {
tasks.add(null);
}
final CountDownLatch latch = new CountDownLatch(tasks.size());
for (int i = 0; i < tasks.size(); i++) {
tasks.set(i, executor.<Void>submit(new Runnable() {
@Override
public void run() {
// Maximize concurrency by blocking this thread until all the other threads are ready to go as well.
latch.countDown();
Uninterruptibles.awaitUninterruptibly(latch);
for (int j = 0; j < iterations; j++) {
assertTrue(func.apply(
list,
new UnaryOperator<ImmutableTreeList<Integer>>() {
@Override
public ImmutableTreeList<Integer> apply(ImmutableTreeList<Integer> l) {
return l.add(l.size());
}
}));
}
}
}, null));
}
try {
Uninterruptibles.getUninterruptibly(Futures.allAsList(tasks));
} catch (ExecutionException ex) {
throw new UncheckedExecutionException(ex);
}
assertEquals(concurrencyLevel * iterations, list.get().size());
for (int i = 0; i < list.get().size(); i++) {
assertEquals(i, (int)list.get().get(i));
}
}
});
} finally {
executor.shutdownNow();
}
}