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

The following examples show how to use java.util.concurrent.Executors#callable() . These examples are extracted from open source projects. 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
/**
 * 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
/**
 * 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
/**
 * 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 Project: j2objc   File: ForkJoinPoolTest.java    License: 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
/**
 * callable(null Runnable) throws NPE
 */
public void testCallableNPE1() {
    try {
        Callable c = Executors.callable((Runnable) null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 6
Source Project: j2objc   File: ExecutorsTest.java    License: 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
/**
 * callable(null PrivilegedExceptionAction) throws NPE
 */
public void testCallableNPE4() {
    try {
        Callable c = Executors.callable((PrivilegedExceptionAction) null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
Example 8
@Override
public void call(Runnable runnable)
    throws Exception {
  Callable<Object> callable = Executors.callable(runnable);
  call(callable);
}
 
Example 9
private Task(Runnable task, V result, long period, long delay, TimeUnit unit) {
  this(Executors.callable(task, result), period, delay, unit);
}
 
Example 10
private Task(Runnable task, V result) {
  this(Executors.callable(task, result));
}
 
Example 11
@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 12
@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 13
@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
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 15
Source Project: j2objc   File: ExecutorsTest.java    License: 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 16
/**
 * 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 17
private FixedRateScheduledFuture(Runnable task, long delay, long period, TimeUnit unit) {
    super(Executors.callable(task), delay, unit);
    this.initialDelay = delay;
    this.period = period;
}
 
Example 18
/**
 * 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 19
/**
 * 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
/**
 * 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));
}