本文整理汇总了Java中java.util.concurrent.ExecutorService.invokeAny方法的典型用法代码示例。如果您正苦于以下问题:Java ExecutorService.invokeAny方法的具体用法?Java ExecutorService.invokeAny怎么用?Java ExecutorService.invokeAny使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ExecutorService
的用法示例。
在下文中一共展示了ExecutorService.invokeAny方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testTimedInvokeAny5
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* timed invokeAny(c) returns result of some task
*/
public void testTimedInvokeAny5() throws Exception {
final ExecutorService e =
new CustomTPE(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(e)) {
long startTime = System.nanoTime();
List<Callable<String>> l = new ArrayList<>();
l.add(new StringTask());
l.add(new StringTask());
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
assertSame(TEST_STRING, result);
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
示例2: testInvokeAny4
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* invokeAny(c) throws ExecutionException if no task completes
*/
public void testInvokeAny4() 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 NPETask());
try {
e.invokeAny(l);
shouldThrow();
} catch (ExecutionException success) {
assertTrue(success.getCause() instanceof NullPointerException);
}
}
}
示例3: testTimedInvokeAny4
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* timed invokeAny(c) throws ExecutionException if no task completes
*/
public void testTimedInvokeAny4() throws Exception {
final ExecutorService e =
new CustomTPE(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(e)) {
long startTime = System.nanoTime();
List<Callable<String>> l = new ArrayList<>();
l.add(new NPETask());
try {
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (ExecutionException success) {
assertTrue(success.getCause() instanceof NullPointerException);
}
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
示例4: testTimedInvokeAny5
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* timed invokeAny(c) returns result of some task
*/
public void testTimedInvokeAny5() throws Exception {
final ExecutorService e =
new ThreadPoolExecutor(2, 2,
LONG_DELAY_MS, MILLISECONDS,
new ArrayBlockingQueue<Runnable>(10));
try (PoolCleaner cleaner = cleaner(e)) {
long startTime = System.nanoTime();
List<Callable<String>> l = new ArrayList<>();
l.add(new StringTask());
l.add(new StringTask());
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
assertSame(TEST_STRING, result);
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
示例5: testTimedInvokeAny4
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* timed invokeAny(c) throws ExecutionException if no task completes
*/
public void testTimedInvokeAny4() throws Exception {
final ExecutorService e = new ScheduledThreadPoolExecutor(2);
try (PoolCleaner cleaner = cleaner(e)) {
long startTime = System.nanoTime();
List<Callable<String>> l = new ArrayList<>();
l.add(new NPETask());
try {
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
shouldThrow();
} catch (ExecutionException success) {
assertTrue(success.getCause() instanceof NullPointerException);
}
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
示例6: testInvokeAny6
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* invokeAny(c) returns result of some task in c if at least one completes
*/
public void testInvokeAny6() throws Throwable {
ExecutorService e = new ForkJoinPool(1);
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(new StringTask());
l.add(new StringTask());
String result = e.invokeAny(l);
assertSame(TEST_STRING, result);
}
}
示例7: testTimedInvokeAny3
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* timed invokeAny(c) throws NPE if c has null elements
*/
public void testTimedInvokeAny3() throws Exception {
final ExecutorService e = new DirectExecutorService();
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<Long>> l = new ArrayList<>();
l.add(new Callable<Long>() {
public Long call() { throw new ArithmeticException(); }});
l.add(null);
try {
e.invokeAny(l, randomTimeout(), randomTimeUnit());
shouldThrow();
} catch (NullPointerException success) {}
}
}
示例8: testInvokeAny
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
@Test
public void testInvokeAny() throws InterruptedException, ExecutionException {
ExecutorService executorService = toTraced(Executors.newFixedThreadPool(NUMBER_OF_THREADS));
MockSpan parentSpan = mockTracer.buildSpan("foo").startManual();
mockTracer.scopeManager().activate(parentSpan, true);
executorService.invokeAny(Arrays.asList(new TestCallable()));
countDownLatch.await();
assertParentSpan(parentSpan);
assertEquals(1, mockTracer.finishedSpans().size());
}
示例9: testInvokeAny1
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* invokeAny(null) throws NPE
*/
public void testInvokeAny1() throws Exception {
final ExecutorService e = new CustomExecutor(2);
try (PoolCleaner cleaner = cleaner(e)) {
try {
e.invokeAny(null);
shouldThrow();
} catch (NullPointerException success) {}
}
}
示例10: testTimedInvokeAny3
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* timed invokeAny(c) throws NPE if c has null elements
*/
public void testTimedInvokeAny3() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
final ExecutorService e = new ScheduledThreadPoolExecutor(2);
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(latchAwaitingStringTask(latch));
l.add(null);
try {
e.invokeAny(l, randomTimeout(), randomTimeUnit());
shouldThrow();
} catch (NullPointerException success) {}
latch.countDown();
}
}
示例11: testInvokeAny3
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* invokeAny(c) throws NPE if c has null elements
*/
public void testInvokeAny3() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ExecutorService e = new CustomExecutor(2);
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(latchAwaitingStringTask(latch));
l.add(null);
try {
e.invokeAny(l);
shouldThrow();
} catch (NullPointerException success) {}
latch.countDown();
}
}
示例12: testTimedInvokeAny5
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* timed invokeAny(c) returns result of some task
*/
public void testTimedInvokeAny5() throws Exception {
final ExecutorService e = new ScheduledThreadPoolExecutor(2);
try (PoolCleaner cleaner = cleaner(e)) {
long startTime = System.nanoTime();
List<Callable<String>> l = new ArrayList<>();
l.add(new StringTask());
l.add(new StringTask());
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
assertSame(TEST_STRING, result);
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
}
}
示例13: testInvokeAny4
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* invokeAny(c) throws ExecutionException if no task completes
*/
public void testInvokeAny4() throws Exception {
final ExecutorService e = new CustomExecutor(2);
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(new NPETask());
try {
e.invokeAny(l);
shouldThrow();
} catch (ExecutionException success) {
assertTrue(success.getCause() instanceof NullPointerException);
}
}
}
示例14: testInvokeAny5
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* invokeAny(c) returns result of some task in c if at least one completes
*/
public void testInvokeAny5() throws Exception {
final ExecutorService e = new DirectExecutorService();
try (PoolCleaner cleaner = cleaner(e)) {
List<Callable<String>> l = new ArrayList<>();
l.add(new StringTask());
l.add(new StringTask());
String result = e.invokeAny(l);
assertSame(TEST_STRING, result);
}
}
示例15: testTimedInvokeAny1
import java.util.concurrent.ExecutorService; //导入方法依赖的package包/类
/**
* timed invokeAny(null) throws NPE
*/
public void testTimedInvokeAny1() throws Exception {
final ExecutorService e = new ScheduledThreadPoolExecutor(2);
try (PoolCleaner cleaner = cleaner(e)) {
try {
e.invokeAny(null, randomTimeout(), randomTimeUnit());
shouldThrow();
} catch (NullPointerException success) {}
}
}