< prev index next > src/java.base/share/classes/java/util/concurrent/ThreadPerTaskExecutor.java
Print this page
* created (but not started) when the Future is created. The thread
* is interrupted when the future is cancelled. The executor is
* notified when the task completes.
*/
private static class ThreadBoundFuture<T>
! extends CompletableFuture<T> implements Runnable {
final ThreadPerTaskExecutor executor;
- final Callable<T> task;
final Thread thread;
ThreadBoundFuture(ThreadPerTaskExecutor executor, Callable<T> task) {
this.executor = executor;
- this.task = task;
this.thread = executor.newThread(this);
}
Thread thread() {
return thread;
}
@Override
! public void run() {
! if (Thread.currentThread() != thread) {
- // should not happen except where something casts this object
- // to a Runnable and invokes the run method.
- throw new WrongThreadException();
- }
- try {
- T result = task.call();
- complete(result);
- } catch (Throwable e) {
- completeExceptionally(e);
- } finally {
- executor.taskComplete(thread);
- }
- }
-
- @Override
- public boolean cancel(boolean mayInterruptIfRunning) {
- boolean cancelled = super.cancel(mayInterruptIfRunning);
- if (cancelled && mayInterruptIfRunning)
- thread.interrupt();
- return cancelled;
}
}
@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
* created (but not started) when the Future is created. The thread
* is interrupted when the future is cancelled. The executor is
* notified when the task completes.
*/
private static class ThreadBoundFuture<T>
! extends FutureTask<T> implements Runnable {
final ThreadPerTaskExecutor executor;
final Thread thread;
ThreadBoundFuture(ThreadPerTaskExecutor executor, Callable<T> task) {
+ super(task);
this.executor = executor;
this.thread = executor.newThread(this);
}
Thread thread() {
return thread;
}
@Override
! protected void done() {
! executor.taskComplete(thread);
}
}
@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
< prev index next >