本文整理汇总了Java中java.util.concurrent.ExecutorService.invokeAll方法的典型用法代码示例。如果您正苦于以下问题:Java ExecutorService.invokeAll方法的具体用法?Java ExecutorService.invokeAll怎么用?Java ExecutorService.invokeAll使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ExecutorService
的用法示例。
在下文中一共展示了ExecutorService.invokeAll方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testTimedInvokeAll4
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* get of element of invokeAll(c) throws exception on failed task
*/
public void testTimedInvokeAll4() throws Exception {
final ExecutorService e = new ScheduledThreadPoolExecutor(2);
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(new NPETask());
List<Future<String>> futures =
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
assertEquals(1, futures.size());
try {
futures.get(0).get();
shouldThrow();
} catch (ExecutionException success) {
assertTrue(success.getCause() instanceof NullPointerException);
}
}
}
示例2: apply
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
@Override
public Set<NitriteId> apply(final NitriteMap<NitriteId, Document> documentMap) {
Set<NitriteId> result = new LinkedHashSet<>();
ExecutorService executorService = nitriteService.getNitriteContext().getWorkerPool();
try {
List<Callable<Set<NitriteId>>> tasks = createTasks(filters, documentMap);
List<Future<Set<NitriteId>>> futures = executorService.invokeAll(tasks);
for (Future<Set<NitriteId>> future : futures) {
Set<NitriteId> nitriteIds = future.get();
if (nitriteIds != null) {
result.addAll(nitriteIds);
}
}
} catch (FilterException fe) {
throw fe;
} catch (Throwable t) {
throw new FilterException(INVALID_OR_FILTER, t);
}
return result;
}
示例3: testTimedInvokeAll3
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* timed invokeAll(c) throws NPE if c has null elements
*/
public void testTimedInvokeAll3() throws Exception {
final ExecutorService e =
new CustomTPE(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(new StringTask());
l.add(null);
try {
e.invokeAll(l, randomTimeout(), randomTimeUnit());
shouldThrow();
} catch (NullPointerException success) {}
}
}
示例4: testInvokeAll4
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* get of element of invokeAll(c) throws exception on failed task
*/
public void testInvokeAll4() throws Exception {
final ExecutorService e =
new CustomTPE(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(new NPETask());
List<Future<String>> futures = e.invokeAll(l);
assertEquals(1, futures.size());
try {
futures.get(0).get();
shouldThrow();
} catch (ExecutionException success) {
assertTrue(success.getCause() instanceof NullPointerException);
}
}
}
示例5: testTimedInvokeAll4
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* get of returned element of invokeAll(c) throws exception on failed task
*/
public void testTimedInvokeAll4() throws Exception {
final ExecutorService e = new DirectExecutorService();
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(new NPETask());
List<Future<String>> futures =
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
assertEquals(1, futures.size());
try {
futures.get(0).get();
shouldThrow();
} catch (ExecutionException success) {
assertTrue(success.getCause() instanceof NullPointerException);
}
}
}
示例6: testInvokeAll5
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* invokeAll(c) returns results of all completed tasks
*/
public void testInvokeAll5() throws Exception {
final ExecutorService e =
new CustomTPE(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(new StringTask());
l.add(new StringTask());
List<Future<String>> futures = e.invokeAll(l);
assertEquals(2, futures.size());
for (Future<String> future : futures)
assertSame(TEST_STRING, future.get());
}
}
示例7: testTimedInvokeAll4
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* get of element of invokeAll(c) throws exception on failed task
*/
public void testTimedInvokeAll4() throws Exception {
final ExecutorService e = new CustomExecutor(2);
final Collection<Callable<String>> c = new ArrayList<>();
c.add(new NPETask());
try (PoolCleaner cleaner = cleaner(e)) {
List<Future<String>> futures =
e.invokeAll(c, LONG_DELAY_MS, MILLISECONDS);
assertEquals(1, futures.size());
try {
futures.get(0).get();
shouldThrow();
} catch (ExecutionException success) {
assertTrue(success.getCause() instanceof NullPointerException);
}
}
}
示例8: testInvokeAll5
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* invokeAll(c) returns results of all completed tasks
*/
public void testInvokeAll5() throws Exception {
final ExecutorService e = new CustomExecutor(2);
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(new StringTask());
l.add(new StringTask());
List<Future<String>> futures = e.invokeAll(l);
assertEquals(2, futures.size());
for (Future<String> future : futures)
assertSame(TEST_STRING, future.get());
}
}
示例9: testTimedInvokeAllNullTimeUnit
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* timed invokeAll(,,null) throws NullPointerException
*/
public void testTimedInvokeAllNullTimeUnit() throws Exception {
final ExecutorService e = new CustomExecutor(2);
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(new StringTask());
try {
e.invokeAll(l, randomTimeout(), null);
shouldThrow();
} catch (NullPointerException success) {}
}
}
示例10: testTimedInvokeAll6
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* timed invokeAll(c) cancels tasks not completed by timeout
*/
public void testTimedInvokeAll6() throws Exception {
for (long timeout = timeoutMillis();;) {
final CountDownLatch done = new CountDownLatch(1);
final Callable<String> waiter = new CheckedCallable<String>() {
public String realCall() {
try { done.await(LONG_DELAY_MS, MILLISECONDS); }
catch (InterruptedException ok) {}
return "1"; }};
final ExecutorService p = new CustomExecutor(2);
try (PoolCleaner cleaner = cleaner(p, done)) {
List<Callable<String>> tasks = new ArrayList<>();
tasks.add(new StringTask("0"));
tasks.add(waiter);
tasks.add(new StringTask("2"));
long startTime = System.nanoTime();
List<Future<String>> futures =
p.invokeAll(tasks, timeout, MILLISECONDS);
assertEquals(tasks.size(), futures.size());
assertTrue(millisElapsedSince(startTime) >= timeout);
for (Future future : futures)
assertTrue(future.isDone());
assertTrue(futures.get(1).isCancelled());
try {
assertEquals("0", futures.get(0).get());
assertEquals("2", futures.get(2).get());
break;
} catch (CancellationException retryWithLongerTimeout) {
timeout *= 2;
if (timeout >= LONG_DELAY_MS / 2)
fail("expected exactly one task to be cancelled");
}
}
}
}
示例11: testInvokeAll2
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* invokeAll(empty collection) returns empty list
*/
public void testInvokeAll2() throws Exception {
final ExecutorService e = new CustomExecutor(2);
final Collection<Callable<String>> emptyCollection
= Collections.emptyList();
try (PoolCleaner cleaner = cleaner(e)) {
List<Future<String>> r = e.invokeAll(emptyCollection);
assertTrue(r.isEmpty());
}
}
示例12: testCreatedFlagParallelExecution
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
public void testCreatedFlagParallelExecution() throws Exception {
createIndex("test");
ensureGreen();
int threadCount = 20;
final int docCount = 300;
int taskCount = docCount * threadCount;
final AtomicIntegerArray createdCounts = new AtomicIntegerArray(docCount);
ExecutorService threadPool = Executors.newFixedThreadPool(threadCount);
List<Callable<Void>> tasks = new ArrayList<>(taskCount);
final Random random = random();
for (int i=0;i< taskCount; i++ ) {
tasks.add(new Callable<Void>() {
@Override
public Void call() throws Exception {
int docId = random.nextInt(docCount);
IndexResponse indexResponse = index("test", "type", Integer.toString(docId), "field1", "value");
if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
createdCounts.incrementAndGet(docId);
}
return null;
}
});
}
threadPool.invokeAll(tasks);
for (int i=0;i<docCount;i++) {
assertThat(createdCounts.get(i), lessThanOrEqualTo(1));
}
terminate(threadPool);
}
示例13: testTimedInvokeAllNullTimeUnit
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* timed invokeAll(,,null) throws NPE
*/
public void testTimedInvokeAllNullTimeUnit() throws Exception {
final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(new StringTask());
try {
e.invokeAll(l, randomTimeout(), null);
shouldThrow();
} catch (NullPointerException success) {}
}
}
示例14: testTimedInvokeAll1
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* timed invokeAll(null) throws NullPointerException
*/
public void testTimedInvokeAll1() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
try (PoolCleaner cleaner = cleaner(e)) {
try {
e.invokeAll(null, randomTimeout(), randomTimeUnit());
shouldThrow();
} catch (NullPointerException success) {}
}
}
示例15: testTimedInvokeAllNullTimeUnit
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* timed invokeAll(,,null) throws NPE
*/
public void testTimedInvokeAllNullTimeUnit() throws Exception {
final ExecutorService e = new ScheduledThreadPoolExecutor(2);
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(new StringTask());
try {
e.invokeAll(l, randomTimeout(), null);
shouldThrow();
} catch (NullPointerException success) {}
}
}