當前位置: 首頁>>代碼示例>>Java>>正文


Java Executors.callable方法代碼示例

本文整理匯總了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"));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,代碼來源:ForkJoinPoolTest.java

示例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"));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:24,代碼來源:ForkJoinPoolTest.java

示例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);
}
 
開發者ID:Netflix,項目名稱:sstable-adaptor,代碼行數:10,代碼來源:DebuggableThreadPoolExecutor.java

示例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);
}
 
開發者ID:Netflix,項目名稱:sstable-adaptor,代碼行數:8,代碼來源:DebuggableThreadPoolExecutor.java

示例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));
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:12,代碼來源:TestScheduledFuture.java

示例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"));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:18,代碼來源:ForkJoinPoolTest.java

示例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) {}
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:10,代碼來源:ExecutorsTest.java

示例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) {}
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:10,代碼來源:ExecutorsTest.java

示例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) {}
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:10,代碼來源:ExecutorsTest.java

示例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) {}
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:10,代碼來源:ExecutorsTest.java

示例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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:8,代碼來源:ExecutorsTest.java

示例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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:8,代碼來源:ExecutorsTest.java

示例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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:9,代碼來源:ExecutorsTest.java

示例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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:9,代碼來源:ExecutorsTest.java

示例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));
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:15,代碼來源:TrustedListenableFutureTask.java


注:本文中的java.util.concurrent.Executors.callable方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。