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

The following examples show how to use java.util.concurrent.Executors#callable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Scheduler.java    From The-5zig-Mod with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Posts a runnable to the Main Server Thread.
 * <p/>
 *
 * @param runnable The runnable that should be executed.
 * @return a Listenable Future.
 */
public ListenableFuture postToMainThread(Runnable runnable, boolean noThreadCheck, int delay) {
	Callable callable = Executors.callable(runnable);
	ListenableFuture listenableFuture = ListenableFutureTask.create(callable);
	if (noThreadCheck || delay > 0 || !The5zigMod.getVars().isMainThread()) {
		synchronized (jobs) {
			jobs.add(new Task(listenableFuture, delay));
			return listenableFuture;
		}
	} else {
		try {
			return Futures.immediateFuture(callable.call());
		} catch (Exception e) {
			return Futures.immediateFailedCheckedFuture(e);
		}
	}
}
 
Example 2
Source File: ForkJoinPoolTest.java    From streamsupport with GNU General Public License v2.0 6 votes vote down vote up
/**
 * A submitted privileged exception action runs to completion
 */
public void testSubmitPrivilegedExceptionAction() throws Exception {
    final Callable<Object> callable =
        Executors.callable(new PrivilegedExceptionAction<Object>() {
            public Object run() { return TEST_STRING; }});
    Runnable r = new CheckedRunnable() {
    public void realRun() throws Exception {
        ExecutorService e = new ForkJoinPool(1);
        PoolCleaner cleaner = null;
        try {
            cleaner = cleaner(e);
            Future<?> future = e.submit(callable);
            assertSame(TEST_STRING, future.get());
        } finally {
            if (cleaner != null) {
                cleaner.close();
            }
        }
    }};

    runWithPermissions(r, new RuntimePermission("modifyThread"));
}
 
Example 3
Source File: ForkJoinPoolTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 4
Source File: ForkJoinPoolTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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 5
Source File: ExecutorsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * callable(null PrivilegedExceptionAction) throws NPE
 */
public void testCallableNPE4() {
    try {
        Callable c = Executors.callable((PrivilegedExceptionAction) null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 6
Source File: ExecutorsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * callable(null, result) throws NPE
 */
public void testCallableNPE2() {
    try {
        Callable c = Executors.callable((Runnable) null, one);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 7
Source File: ExecutorsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * callable(null Runnable) throws NPE
 */
public void testCallableNPE1() {
    try {
        Callable c = Executors.callable((Runnable) null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 8
Source File: ExecutorsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 9
Source File: XnioEventLoop.java    From netty-xnio-transport with Apache License 2.0 4 votes vote down vote up
private FixedRateScheduledFuture(Runnable task, long delay, long period, TimeUnit unit) {
    super(Executors.callable(task), delay, unit);
    this.initialDelay = delay;
    this.period = period;
}
 
Example 10
Source File: ExecutorsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 11
Source File: ExecutorsTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * 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 12
Source File: SQSExecutorService.java    From amazon-sqs-java-temporary-queues-client with Apache License 2.0 4 votes vote down vote up
private <T> SQSFutureTask<T> newTaskFor(Runnable runnable, T value, boolean withResponse) {
    MessageContent messageContent = toMessageContent(runnable);
    addDeduplicationAttributes(messageContent, runnable);
    return new SQSFutureTask<>(Executors.callable(runnable, value), messageContent, withResponse);
}
 
Example 13
Source File: MockExecutorService.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
@Override
public <T> MockExecutorJob<T> submit(Runnable task, T result) {
	MockExecutorJob<T> job = new OneTimeJob<>(Executors.callable(task, result));
	jobs.add(job);
	return job;
}
 
Example 14
Source File: MockExecutorService.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
@Override
public MockExecutorJob<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
	RepeatingJob<?> job = new RepeatingJob<>(Executors.callable(command), unit.toMillis(initialDelay), unit.toMillis(delay));
	jobs.add(job);
	return job;
}
 
Example 15
Source File: MockExecutorService.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
@Override
public MockExecutorJob<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
	RepeatingJob<?> job = new RepeatingJob<>(Executors.callable(command), unit.toMillis(initialDelay), unit.toMillis(period));
	jobs.add(job);
	return job;
}
 
Example 16
Source File: TaskExecutorService.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
private Task(Runnable task, V result) {
  this(Executors.callable(task, result));
}
 
Example 17
Source File: TaskExecutorService.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
private Task(Runnable task, V result, long period, long delay, TimeUnit unit) {
  this(Executors.callable(task, result), period, delay, unit);
}
 
Example 18
Source File: DefaultServiceCallWrapper.java    From openAGV with Apache License 2.0 4 votes vote down vote up
@Override
public void call(Runnable runnable)
    throws Exception {
  Callable<Object> callable = Executors.callable(runnable);
  call(callable);
}
 
Example 19
Source File: TrustedListenableFutureTask.java    From codebuff with BSD 2-Clause "Simplified" License votes vote down vote up
/**
 * 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 20
Source File: TrustedListenableFutureTask.java    From codebuff with BSD 2-Clause "Simplified" License votes vote down vote up
/**
 * 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));
}