Java Code Examples for java.util.concurrent.Executors.callable()

The following are Jave code examples for showing how to use callable() of the java.util.concurrent.Executors class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: openjdk-jdk10   File: ForkJoinPoolTest.java   View Source Code Vote up 6 votes
/**
 * 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"));
}
 
Example 2
Project: openjdk-jdk10   File: ForkJoinPoolTest.java   View Source Code Vote up 6 votes
/**
 * 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"));
}
 
Example 3
Project: sstable-adaptor   File: DebuggableThreadPoolExecutor.java   View Source Code Vote up 5 votes
@Override
public void execute(Runnable command)
{
    super.execute(!(command instanceof LocalSessionWrapper)
            ? new LocalSessionWrapper<Object>(Executors.callable(command, null))
            : command);
}
 
Example 4
Project: sstable-adaptor   File: DebuggableThreadPoolExecutor.java   View Source Code Vote up 5 votes
@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);
}
 
Example 5
Project: GitHub   File: TestScheduledFuture.java   View Source Code Vote up 5 votes
TestScheduledFuture(
    FakeClock fakeClock,
    ScheduledQueue scheduledQueue,
    long delay,
    final Runnable runnable) {
  this(
      fakeClock,
      scheduledQueue,
      delay,
      Executors.<V>callable(runnable, null));
}
 
Example 6
Project: openjdk-jdk10   File: ForkJoinPoolTest.java   View Source Code Vote up 5 votes
/**
 * 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"));
}
 
Example 7
Project: openjdk-jdk10   File: ExecutorsTest.java   View Source Code Vote up 5 votes
/**
 * callable(null Runnable) throws NPE
 */
public void testCallableNPE1() {
    try {
        Callable c = Executors.callable((Runnable) null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 8
Project: openjdk-jdk10   File: ExecutorsTest.java   View Source Code Vote up 5 votes
/**
 * callable(null, result) throws NPE
 */
public void testCallableNPE2() {
    try {
        Callable c = Executors.callable((Runnable) null, one);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 9
Project: openjdk-jdk10   File: ExecutorsTest.java   View Source Code Vote up 5 votes
/**
 * callable(null PrivilegedAction) throws NPE
 */
public void testCallableNPE3() {
    try {
        Callable c = Executors.callable((PrivilegedAction) null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 10
Project: openjdk-jdk10   File: ExecutorsTest.java   View Source Code Vote up 5 votes
/**
 * callable(null PrivilegedExceptionAction) throws NPE
 */
public void testCallableNPE4() {
    try {
        Callable c = Executors.callable((PrivilegedExceptionAction) null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 11
Project: openjdk-jdk10   File: ExecutorsTest.java   View Source Code Vote up 4 votes
/**
 * callable(Runnable) returns null when called
 */
public void testCallable1() throws Exception {
    Callable c = Executors.callable(new NoOpRunnable());
    assertNull(c.call());
}
 
Example 12
Project: openjdk-jdk10   File: ExecutorsTest.java   View Source Code Vote up 4 votes
/**
 * callable(Runnable, result) returns result when called
 */
public void testCallable2() throws Exception {
    Callable c = Executors.callable(new NoOpRunnable(), one);
    assertSame(one, c.call());
}
 
Example 13
Project: openjdk-jdk10   File: ExecutorsTest.java   View Source Code Vote up 4 votes
/**
 * 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());
}
 
Example 14
Project: openjdk-jdk10   File: ExecutorsTest.java   View Source Code Vote up 4 votes
/**
 * 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());
}
 
Example 15
Project: guava-mock   File: TrustedListenableFutureTask.java   View Source Code Vote up 2 votes
/**
 * 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));
}
 
Example 16
Project: googles-monorepo-demo   File: TrustedListenableFutureTask.java   View Source Code Vote up 2 votes
/**
 * 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));
}