本文整理匯總了Java中java.util.concurrent.Executors.callable方法的典型用法代碼示例。如果您正苦於以下問題:Java Executors.callable方法的具體用法?Java Executors.callable怎麽用?Java Executors.callable使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.Executors
的用法示例。
在下文中一共展示了Executors.callable方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testSubmitPrivilegedExceptionAction
import java.util.concurrent.Executors; //導入方法依賴的package包/類
/**
* A submitted privileged exception action runs to completion
*/
public void testSubmitPrivilegedExceptionAction() throws Exception {
final Callable callable =
Executors.callable(new PrivilegedExceptionAction() {
public Object run() { return TEST_STRING; }});
Runnable r = new CheckedRunnable() {
public void realRun() throws Exception {
ExecutorService e = new ForkJoinPool(1);
try (PoolCleaner cleaner = cleaner(e)) {
Future future = e.submit(callable);
assertSame(TEST_STRING, future.get());
}
}};
runWithPermissions(r, new RuntimePermission("modifyThread"));
}
示例2: testSubmitFailedPrivilegedExceptionAction
import java.util.concurrent.Executors; //導入方法依賴的package包/類
/**
* A submitted failed privileged exception action reports exception
*/
public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
final Callable callable =
Executors.callable(new PrivilegedExceptionAction() {
public Object run() { throw new IndexOutOfBoundsException(); }});
Runnable r = new CheckedRunnable() {
public void realRun() throws Exception {
ExecutorService e = new ForkJoinPool(1);
try (PoolCleaner cleaner = cleaner(e)) {
Future future = e.submit(callable);
try {
future.get();
shouldThrow();
} catch (ExecutionException success) {
assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
}
}
}};
runWithPermissions(r, new RuntimePermission("modifyThread"));
}
示例3: newTaskFor
import java.util.concurrent.Executors; //導入方法依賴的package包/類
@Override
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T result)
{
if (!(runnable instanceof LocalSessionWrapper))
{
return new LocalSessionWrapper<T>(Executors.callable(runnable, result));
}
return super.newTaskFor(runnable, result);
}
示例4: execute
import java.util.concurrent.Executors; //導入方法依賴的package包/類
@Override
public void execute(Runnable command)
{
super.execute(!(command instanceof LocalSessionWrapper)
? new LocalSessionWrapper<Object>(Executors.callable(command, null))
: command);
}
示例5: TestScheduledFuture
import java.util.concurrent.Executors; //導入方法依賴的package包/類
TestScheduledFuture(
FakeClock fakeClock,
ScheduledQueue scheduledQueue,
long delay,
final Runnable runnable) {
this(
fakeClock,
scheduledQueue,
delay,
Executors.<V>callable(runnable, null));
}
示例6: testSubmitPrivilegedAction
import java.util.concurrent.Executors; //導入方法依賴的package包/類
/**
* A submitted privileged action runs to completion
*/
public void testSubmitPrivilegedAction() throws Exception {
final Callable callable = Executors.callable(new PrivilegedAction() {
public Object run() { return TEST_STRING; }});
Runnable r = new CheckedRunnable() {
public void realRun() throws Exception {
ExecutorService e = new ForkJoinPool(1);
try (PoolCleaner cleaner = cleaner(e)) {
Future future = e.submit(callable);
assertSame(TEST_STRING, future.get());
}
}};
runWithPermissions(r, new RuntimePermission("modifyThread"));
}
示例7: testCallableNPE1
import java.util.concurrent.Executors; //導入方法依賴的package包/類
/**
* callable(null Runnable) throws NPE
*/
public void testCallableNPE1() {
try {
Callable c = Executors.callable((Runnable) null);
shouldThrow();
} catch (NullPointerException success) {}
}
示例8: testCallableNPE2
import java.util.concurrent.Executors; //導入方法依賴的package包/類
/**
* callable(null, result) throws NPE
*/
public void testCallableNPE2() {
try {
Callable c = Executors.callable((Runnable) null, one);
shouldThrow();
} catch (NullPointerException success) {}
}
示例9: testCallableNPE3
import java.util.concurrent.Executors; //導入方法依賴的package包/類
/**
* callable(null PrivilegedAction) throws NPE
*/
public void testCallableNPE3() {
try {
Callable c = Executors.callable((PrivilegedAction) null);
shouldThrow();
} catch (NullPointerException success) {}
}
示例10: testCallableNPE4
import java.util.concurrent.Executors; //導入方法依賴的package包/類
/**
* callable(null PrivilegedExceptionAction) throws NPE
*/
public void testCallableNPE4() {
try {
Callable c = Executors.callable((PrivilegedExceptionAction) null);
shouldThrow();
} catch (NullPointerException success) {}
}
示例11: testCallable1
import java.util.concurrent.Executors; //導入方法依賴的package包/類
/**
* callable(Runnable) returns null when called
*/
public void testCallable1() throws Exception {
Callable c = Executors.callable(new NoOpRunnable());
assertNull(c.call());
}
示例12: testCallable2
import java.util.concurrent.Executors; //導入方法依賴的package包/類
/**
* callable(Runnable, result) returns result when called
*/
public void testCallable2() throws Exception {
Callable c = Executors.callable(new NoOpRunnable(), one);
assertSame(one, c.call());
}
示例13: testCallable3
import java.util.concurrent.Executors; //導入方法依賴的package包/類
/**
* callable(PrivilegedAction) returns its result when called
*/
public void testCallable3() throws Exception {
Callable c = Executors.callable(new PrivilegedAction() {
public Object run() { return one; }});
assertSame(one, c.call());
}
示例14: testCallable4
import java.util.concurrent.Executors; //導入方法依賴的package包/類
/**
* callable(PrivilegedExceptionAction) returns its result when called
*/
public void testCallable4() throws Exception {
Callable c = Executors.callable(new PrivilegedExceptionAction() {
public Object run() { return one; }});
assertSame(one, c.call());
}
示例15: create
import java.util.concurrent.Executors; //導入方法依賴的package包/類
/**
* Creates a {@code ListenableFutureTask} that will upon running, execute the given
* {@code Runnable}, and arrange that {@code get} will return the given result on successful
* completion.
*
* @param runnable the runnable task
* @param result the result to return on successful completion. If you don't need a particular
* result, consider using constructions of the form:
* {@code ListenableFuture<?> f = ListenableFutureTask.create(runnable,
* null)}
*/
static <V> TrustedListenableFutureTask<V> create(Runnable runnable, @Nullable V result) {
return new TrustedListenableFutureTask<V>(Executors.callable(runnable, result));
}