APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.util.concurrent

pack-priv Class ThreadPerTaskExecutor

extends ThreadContainer
implements ExecutorService
Class Inheritance
All Implemented Interfaces
java.util.concurrent.ExecutorService, java.lang.AutoCloseable, java.util.concurrent.Executor
Imports
java.lang.invoke.MethodHandles, .VarHandle, java.util.ArrayList, .Collection, .Iterator, .List, .Objects, .Set, java.util.concurrent.locks.LockSupport, java.util.stream.Stream, jdk.internal.access.JavaLangAccess, .SharedSecrets, jdk.internal.invoke.MhUtil, jdk.internal.vm.ThreadContainer, .ThreadContainers

An ExecutorService that starts a new thread for each task. The number of threads is unbounded.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ThreadPerTaskExecutor.AnyResultHolder<T>

An object for use by invokeAny to hold the result of the first task to complete normally and/or the first exception thrown.

private static class
ThreadPerTaskExecutor.TaskRunner

Runs a task and notifies the executor when it completes.

private static class
ThreadPerTaskExecutor.ThreadBoundFuture<T>

A Future for a task that runs in its own thread.

Field Summary

Modifier and TypeField and Description
private final ThreadFactory
private static final JavaLangAccess
private volatile Object
private static final int
private static final int
private static final VarHandle
private volatile int
private static final int
private final CountDownLatch
private final Set<Thread>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public boolean
awaitTermination(long timeout, TimeUnit unit)

Implements java.util.concurrent.ExecutorService.awaitTermination.

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

private void
awaitTermination()

Waits for executor to terminate.

private <T> void
cancelAll(List<Future<T>> futures, int j)

public void
close()

Overrides default java.util.concurrent.ExecutorService.close.

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.

pack-priv static ThreadPerTaskExecutor
create(ThreadFactory factory)

Creates a thread-per-task executor that creates threads using the given factory.

private void
ensureNotShutdown()

Throws RejectedExecutionException if the executor has been shutdown.

public void
execute(Runnable task)

Implements java.util.concurrent.Executor.execute.

Executes the given command at some time in the future.

public <T> List<Future<T>>
invokeAll(Collection<? extends Callable<T>> tasks)

Implements java.util.concurrent.ExecutorService.invokeAll.

Executes the given tasks, returning a list of Futures holding their status and results when all complete.

public <T> List<Future<T>>
invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)

Implements java.util.concurrent.ExecutorService.invokeAll.

Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first.

public <T> T
invokeAny(Collection<? extends Callable<T>> tasks)

Implements java.util.concurrent.ExecutorService.invokeAny.

Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do.

public <T> T
invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)

Implements java.util.concurrent.ExecutorService.invokeAny.

Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do before the given timeout elapses.

private <T> T
invokeAny(Collection<? extends Callable<T>> tasks, boolean timed, long timeout, TimeUnit unit)

public boolean
isShutdown()

Implements java.util.concurrent.ExecutorService.isShutdown.

Returns true if this executor has been shut down.

public boolean
isTerminated()

Implements java.util.concurrent.ExecutorService.isTerminated.

Returns true if all tasks have completed following shut down.

private Thread
newThread(Runnable task)

Creates a thread to run the given task.

public void
shutdown()

Implements java.util.concurrent.ExecutorService.shutdown.

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.

public List<Runnable>
shutdownNow()

Implements java.util.concurrent.ExecutorService.shutdownNow.

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

private void
start(Thread thread)

Adds a thread to the set of threads and starts it.

private Thread
start(Runnable task)

Starts a thread to execute the given task.

public <T> Future<T>
submit(Callable<T> task)

Implements java.util.concurrent.ExecutorService.submit.

Submits a value-returning task for execution and returns a Future representing the pending results of the task.

public Future<?>
submit(Runnable task)

Implements java.util.concurrent.ExecutorService.submit.

Submits a Runnable task for execution and returns a Future representing that task.

public <T> Future<T>
submit(Runnable task, T result)

Implements java.util.concurrent.ExecutorService.submit.

Submits a Runnable task for execution and returns a Future representing that task.

private void
taskComplete(Thread thread)

Notify the executor that the task executed by the given thread is complete.

public long
threadCount()

Overrides jdk.internal.vm.ThreadContainer.threadCount.

Return a count of the number of threads in this container.

public Stream<Thread>
threads()

Implements abstract jdk.internal.vm.ThreadContainer.threads.

Returns a stream of the live threads in this container.

private void
tryShutdownAndTerminate(boolean interruptThreads)

Attempts to shutdown and terminate the executor.

private void
tryTerminate()

Attempts to terminate if already shutdown.

Inherited from jdk.internal.vm.ThreadContainer:
addchildrennameonExitonStartparentremovescopedValueBindingstoString

Field Detail

factoryback to summary
private final ThreadFactory factory
JLAback to summary
private static final JavaLangAccess JLA

Hides jdk.internal.vm.StackableScope.JLA.

keyback to summary
private volatile Object key
RUNNINGback to summary
private static final int RUNNING
SHUTDOWNback to summary
private static final int SHUTDOWN
STATEback to summary
private static final VarHandle STATE
stateback to summary
private volatile int state
TERMINATEDback to summary
private static final int TERMINATED
terminationSignalback to summary
private final CountDownLatch terminationSignal
threadsback to summary
private final Set<Thread> threads

Constructor Detail

ThreadPerTaskExecutorback to summary
private ThreadPerTaskExecutor(ThreadFactory factory)

Method Detail

awaitTerminationback to summary
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException

Implements java.util.concurrent.ExecutorService.awaitTermination.

Doc from java.util.concurrent.ExecutorService.awaitTermination.

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

Annotations
@Override
Exceptions
InterruptedException:
if interrupted while waiting
awaitTerminationback to summary
private void awaitTermination()

Waits for executor to terminate.

cancelAllback to summary
private <T> void cancelAll(List<Future<T>> futures, int j)
closeback to summary
public void close()

Overrides default java.util.concurrent.ExecutorService.close.

Doc from java.util.concurrent.ExecutorService.close.

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. This method waits until all tasks have completed execution and the executor has terminated.

If interrupted while waiting, this method stops all executing tasks as if by invoking shutdownNow(). It then continues to wait until all actively executing tasks have completed. Tasks that were awaiting execution are not executed. The interrupted status will be re-asserted before this method returns.

If already terminated, invoking this method has no effect.

Annotations
@Override
createback to summary
pack-priv static ThreadPerTaskExecutor create(ThreadFactory factory)

Creates a thread-per-task executor that creates threads using the given factory.

ensureNotShutdownback to summary
private void ensureNotShutdown()

Throws RejectedExecutionException if the executor has been shutdown.

executeback to summary
public void execute(Runnable task)

Implements java.util.concurrent.Executor.execute.

Doc from java.util.concurrent.Executor.execute.

Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.

Annotations
@Override
invokeAllback to summary
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException

Implements java.util.concurrent.ExecutorService.invokeAll.

Doc from java.util.concurrent.ExecutorService.invokeAll.

Executes the given tasks, returning a list of Futures holding their status and results when all complete. Future#isDone is true for each element of the returned list. Note that a completed task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress.

Annotations
@Override
Exceptions
InterruptedException:
if interrupted while waiting, in which case unfinished tasks are cancelled
invokeAllback to summary
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException

Implements java.util.concurrent.ExecutorService.invokeAll.

Doc from java.util.concurrent.ExecutorService.invokeAll.

Executes the given tasks, returning a list of Futures holding their status and results when all complete or the timeout expires, whichever happens first. Future#isDone is true for each element of the returned list. Upon return, tasks that have not completed are cancelled. Note that a completed task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress.

Annotations
@Override
Exceptions
InterruptedException:
if interrupted while waiting, in which case unfinished tasks are cancelled
invokeAnyback to summary
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException

Implements java.util.concurrent.ExecutorService.invokeAny.

Doc from java.util.concurrent.ExecutorService.invokeAny.

Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do. Upon normal or exceptional return, tasks that have not completed are cancelled. The results of this method are undefined if the given collection is modified while this operation is in progress.

Annotations
@Override
Exceptions
InterruptedException:
if interrupted while waiting
ExecutionException:
if no task successfully completes
invokeAnyback to summary
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException

Implements java.util.concurrent.ExecutorService.invokeAny.

Doc from java.util.concurrent.ExecutorService.invokeAny.

Executes the given tasks, returning the result of one that has completed successfully (i.e., without throwing an exception), if any do before the given timeout elapses. Upon normal or exceptional return, tasks that have not completed are cancelled. The results of this method are undefined if the given collection is modified while this operation is in progress.

Annotations
@Override
Exceptions
InterruptedException:
if interrupted while waiting
ExecutionException:
if no task successfully completes
TimeoutException:
if the given timeout elapses before any task successfully completes
invokeAnyback to summary
private <T> T invokeAny(Collection<? extends Callable<T>> tasks, boolean timed, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
isShutdownback to summary
public boolean isShutdown()

Implements java.util.concurrent.ExecutorService.isShutdown.

Doc from java.util.concurrent.ExecutorService.isShutdown.

Returns true if this executor has been shut down.

Annotations
@Override
isTerminatedback to summary
public boolean isTerminated()

Implements java.util.concurrent.ExecutorService.isTerminated.

Doc from java.util.concurrent.ExecutorService.isTerminated.

Returns true if all tasks have completed following shut down. Note that isTerminated is never true unless either shutdown or shutdownNow was called first.

Annotations
@Override
newThreadback to summary
private Thread newThread(Runnable task)

Creates a thread to run the given task.

shutdownback to summary
public void shutdown()

Implements java.util.concurrent.ExecutorService.shutdown.

Doc from java.util.concurrent.ExecutorService.shutdown.

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.

This method does not wait for previously submitted tasks to complete execution. Use awaitTermination to do that.

Annotations
@Override
shutdownNowback to summary
public List<Runnable> shutdownNow()

Implements java.util.concurrent.ExecutorService.shutdownNow.

Doc from java.util.concurrent.ExecutorService.shutdownNow.

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

This method does not wait for actively executing tasks to terminate. Use awaitTermination to do that.

There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread#interrupt, so any task that fails to respond to interrupts may never terminate.

Annotations
@Override
startback to summary
private void start(Thread thread)

Adds a thread to the set of threads and starts it.

startback to summary
private Thread start(Runnable task)

Starts a thread to execute the given task.

submitback to summary
public <T> Future<T> submit(Callable<T> task)

Implements java.util.concurrent.ExecutorService.submit.

Doc from java.util.concurrent.ExecutorService.submit.

Submits a value-returning task for execution and returns a Future representing the pending results of the task. The Future's get method will return the task's result upon successful completion.

If you would like to immediately block waiting for a task, you can use constructions of the form result = exec.submit(aCallable).get();

Note

The Executors class includes a set of methods that can convert some other common closure-like objects, for example, java.security.PrivilegedAction to Callable form so they can be submitted.

Annotations
@Override
submitback to summary
public Future<?> submit(Runnable task)

Implements java.util.concurrent.ExecutorService.submit.

Doc from java.util.concurrent.ExecutorService.submit.

Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return null upon successful completion.

Annotations
@Override
submitback to summary
public <T> Future<T> submit(Runnable task, T result)

Implements java.util.concurrent.ExecutorService.submit.

Doc from java.util.concurrent.ExecutorService.submit.

Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return the given result upon successful completion.

Annotations
@Override
taskCompleteback to summary
private void taskComplete(Thread thread)

Notify the executor that the task executed by the given thread is complete. If the executor has been shutdown then this method will attempt to terminate the executor.

threadCountback to summary
public long threadCount()

Overrides jdk.internal.vm.ThreadContainer.threadCount.

Doc from jdk.internal.vm.ThreadContainer.threadCount.

Return a count of the number of threads in this container.

Annotations
@Override
threadsback to summary
public Stream<Thread> threads()

Implements abstract jdk.internal.vm.ThreadContainer.threads.

Doc from jdk.internal.vm.ThreadContainer.threads.

Returns a stream of the live threads in this container.

Annotations
@Override
tryShutdownAndTerminateback to summary
private void tryShutdownAndTerminate(boolean interruptThreads)

Attempts to shutdown and terminate the executor. If interruptThreads is true then all running threads are interrupted.

tryTerminateback to summary
private void tryTerminate()

Attempts to terminate if already shutdown. If this method terminates the executor then it signals any threads that are waiting for termination.

java.util.concurrent back to summary

private Class ThreadPerTaskExecutor.AnyResultHolder<T>

extends Object
Class Inheritance

An object for use by invokeAny to hold the result of the first task to complete normally and/or the first exception thrown. The object also maintains a count of the number of tasks that attempted to complete up to when the first tasks completes normally.

Field Summary

Modifier and TypeField and Description
private static final VarHandle
private volatile Throwable
private static final VarHandle
private volatile int
private static final Object
private final Thread
private static final VarHandle
private volatile T

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv void
complete(T value)

Complete with the given result if not already completed.

pack-priv void
completeExceptionally(Throwable exc)

Complete with the given exception.

pack-priv int
exceptionCount()

Returns the number of tasks that terminated with an exception before a task completed normally.

pack-priv Throwable
firstException()

Returns the first exception thrown if recorded by this object.

pack-priv T
result()

Returns non-null if a task completed successfully.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EXCEPTIONback to summary
private static final VarHandle EXCEPTION
exceptionback to summary
private volatile Throwable exception
EXCEPTION_COUNTback to summary
private static final VarHandle EXCEPTION_COUNT
exceptionCountback to summary
private volatile int exceptionCount
NULLback to summary
private static final Object NULL
ownerback to summary
private final Thread owner
RESULTback to summary
private static final VarHandle RESULT
resultback to summary
private volatile T result

Constructor Detail

AnyResultHolderback to summary
pack-priv AnyResultHolder(Thread owner)

Method Detail

completeback to summary
pack-priv void complete(T value)

Complete with the given result if not already completed. The winner unparks the owner thread.

completeExceptionallyback to summary
pack-priv void completeExceptionally(Throwable exc)

Complete with the given exception. If the result is not already set then it unparks the owner thread.

exceptionCountback to summary
pack-priv int exceptionCount()

Returns the number of tasks that terminated with an exception before a task completed normally.

firstExceptionback to summary
pack-priv Throwable firstException()

Returns the first exception thrown if recorded by this object.

API Note

The result() method should be used to test if there is a result before invoking the exception method.

resultback to summary
pack-priv T result()

Returns non-null if a task completed successfully. The result is NULL if completed with null.

java.util.concurrent back to summary

private Class ThreadPerTaskExecutor.TaskRunner

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable

Runs a task and notifies the executor when it completes.

Field Summary

Modifier and TypeField and Description
pack-priv final ThreadPerTaskExecutor
pack-priv final Runnable

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

executorback to summary
pack-priv final ThreadPerTaskExecutor executor
taskback to summary
pack-priv final Runnable task

Constructor Detail

TaskRunnerback to summary
pack-priv TaskRunner(ThreadPerTaskExecutor executor, Runnable task)

Method Detail

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override
java.util.concurrent back to summary

private Class ThreadPerTaskExecutor.ThreadBoundFuture<T>

extends FutureTask<T>
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable

A Future for a task that runs in its own thread. The thread is 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.

Field Summary

Modifier and TypeField and Description
pack-priv final ThreadPerTaskExecutor
pack-priv final Thread

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
pack-priv Thread
Inherited from java.util.concurrent.FutureTask:
canceldoneexceptionNowgetgetisCancelledisDoneresultNowrunAndResetsetsetExceptionstatetoString

Field Detail

executorback to summary
pack-priv final ThreadPerTaskExecutor executor
threadback to summary
pack-priv final Thread thread

Constructor Detail

ThreadBoundFutureback to summary
pack-priv ThreadBoundFuture(ThreadPerTaskExecutor executor, Callable<T> task)

Method Detail

runback to summary
public void run()

Overrides java.util.concurrent.FutureTask.run.

Implements java.lang.Runnable.run, java.util.concurrent.RunnableFuture.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override
threadback to summary
pack-priv Thread thread()