ThreadFlock defines the open method to open a new flock,
the start method to start a thread in the flock, and the
close method to close the flock. The close waits for all
threads in the flock to finish. The awaitAll method may be used
to wait for all threads to finish without closing the flock. The wakeup()
method will cause awaitAll method to complete early, which can be used to
support cancellation in higher-level APIs. ThreadFlock also defines the shutdown method to prevent new threads from starting while allowing
existing threads in the flock to continue.
Thread flocks are intended to be used in a structured manner. The
thread that opens a new flock is the owner. The owner closes the
flock when done, failure to do so may result in a resource leak. The open
and close should be matched to avoid closing an enclosing flock
while a nested flock is open. A ThreadFlock can be used with the
try-with-resources construct if required but more likely, the close method of a
higher-level API that implements AutoCloseable will close the flock.
Thread flocks are conceptually nodes in a tree. A thread T started in
flock "A" may itself open a new flock "B", implicitly forming a tree where flock
"A" is the parent of flock "B". When nested, say where thread T opens
flock "B" and then invokes a method that opens flock "C", then the enclosing
flock "B" is conceptually the parent of the nested flock "C". ThreadFlock does
not define APIs that exposes the tree structure. It does define the containsThread method to test if a flock contains a
thread, a test that is equivalent to testing membership of flocks in the tree.
The start and shutdown methods are confined to the flock
owner or threads contained in the flock. The confinement check is equivalent to
invoking the containsThread method to test if the caller is contained
in the flock.
Unless otherwise specified, passing a null argument to a method
in this class will cause a NullPointerException to be thrown.
| Modifier and Type | Class and Description |
|---|---|
| private static class | ThreadFlock.
A ThreadContainer backed by a ThreadFlock. |
| Modifier and Type | Field and Description |
|---|---|
| private volatile boolean | |
| private final ThreadFlock. | |
| private static final JavaLangAccess | |
| private final String | |
| private static final VarHandle | |
| private volatile boolean | |
| private final ScopedValueContainer. | |
| private volatile boolean | |
| private static final VarHandle | |
| private volatile int | |
| private final Set |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | Returns: true if there are no threads in the flock, false if wakeup was invoked and there are unfinished threadsWait for all threads in the flock to finish executing their tasks. |
| public boolean | |
| private void | |
| public void | |
| public boolean | Returns: true if this flock contains the threadthe thread thread)Tests if this flock contains the given thread. |
| private void | |
| private void | |
| private void | ensureOwnerOrContainsThread()
Throws WrongThreadException if the current thread is not the owner or a thread contained in the flock. |
| private boolean | getAndSetPermit(boolean newValue)
Sets the wakeup permit to the given value, returning the previous value. |
| private void | |
| public boolean | |
| public boolean | Returns: true if the flock has been shut downReturns true if the flock has been shut down. |
| public String | Returns: the name of this flock ornull if unnamedReturns the name of this flock or |
| private void | |
| private void | |
| public static ThreadFlock | |
| public Thread | |
| private ScopedValueContainer. | |
| public void | shutdown()
Shutdown this flock so that no new threads can be started, existing threads in the flock will continue to run. |
| public Thread | |
| private long | |
| public Stream | Returns: a stream of the threads in this flockReturns a stream of the threads in this flock. |
| public String | |
| public void |
| closed | back to summary |
|---|---|
| private volatile boolean closed | |
| container | back to summary |
|---|---|
| private final ThreadFlock. | |
| JLA | back to summary |
|---|---|
| private static final JavaLangAccess JLA | |
| name | back to summary |
|---|---|
| private final String name | |
| PERMIT | back to summary |
|---|---|
| private static final VarHandle PERMIT | |
| permit | back to summary |
|---|---|
| private volatile boolean permit | |
| scopedValueBindings | back to summary |
|---|---|
| private final ScopedValueContainer. | |
| shutdown | back to summary |
|---|---|
| private volatile boolean shutdown | |
| THREAD_COUNT | back to summary |
|---|---|
| private static final VarHandle THREAD_COUNT | |
| threadCount | back to summary |
|---|---|
| private volatile int threadCount | |
| threads | back to summary |
|---|---|
| private final Set<Thread> threads | |
| ThreadFlock | back to summary |
|---|---|
| pack-priv ThreadFlock(String name) | |
| awaitAll | back to summary |
|---|---|
| public boolean awaitAll() throws InterruptedException Wait for all threads in the flock to finish executing their tasks. This method
waits until all threads finish, the This method may only be invoked by the flock owner. The method trivially returns true when the flock is closed. This method clears the effect of any previous invocations of the
| |
| awaitAll | back to summary |
|---|---|
| public boolean awaitAll(Duration timeout) throws InterruptedException, TimeoutException Wait, up to the given waiting timeout, for all threads in the flock to finish
executing their tasks. This method waits until all threads finish, the This method may only be invoked by the flock owner. The method trivially returns true when the flock is closed. This method clears the effect of any previous invocations of the
| |
| clearPermit | back to summary |
|---|---|
| private void clearPermit() Clear wakeup permit. | |
| close | back to summary |
|---|---|
| public void close() Implements java. Closes this flock. This method first shuts down the flock to prevent new threads from starting. It then waits for the threads in the flock to finish executing their tasks. In other words, this method blocks until all threads in the flock finish. This method may only be invoked by the flock owner. If interrupted then this method continues to wait until all threads finish, before completing with the interrupted status set. A ThreadFlock is intended to be used in a structured manner. If
this method is called to close a flock before nested flocks are closed then it
closes the nested flocks (in the reverse order that they were created in),
closes this flock, and then throws
| |
| containsThread | back to summary |
|---|---|
| public boolean containsThread(Thread thread) Tests if this flock contains the given thread. This method returns
| |
| decrementThreadCount | back to summary |
|---|---|
| private void decrementThreadCount() Decrement the thread count. Unpark the owner if the count goes to zero. | |
| ensureOwner | back to summary |
|---|---|
| private void ensureOwner() Throws WrongThreadException if the current thread is not the owner. | |
| ensureOwnerOrContainsThread | back to summary |
|---|---|
| private void ensureOwnerOrContainsThread() Throws WrongThreadException if the current thread is not the owner or a thread contained in the flock. | |
| getAndSetPermit | back to summary |
|---|---|
| private boolean getAndSetPermit(boolean newValue) Sets the wakeup permit to the given value, returning the previous value. | |
| incrementThreadCount | back to summary |
|---|---|
| private void incrementThreadCount() | |
| isClosed | back to summary |
|---|---|
| public boolean isClosed() Returns true if the flock has been closed.
| |
| isShutdown | back to summary |
|---|---|
| public boolean isShutdown() Returns true if the flock has been shut down.
| |
| name | back to summary |
|---|---|
| public String name() Returns the name of this flock or
| |
| onExit | back to summary |
|---|---|
| private void onExit(Thread thread) Invoked on the terminating thread or the parent thread when starting
| |
| onStart | back to summary |
|---|---|
| private void onStart(Thread thread) Invoked on the parent thread when starting | |
| open | back to summary |
|---|---|
| public static ThreadFlock open(String name) Opens a new thread flock. The flock is owned by the current thread. It can be named to aid debugging. This method captures the current thread's scoped value bindings for inheritance by threads created in the flock. For the purposes of containment, monitoring, and debugging, the parent of the new flock is determined as follows:
| |
| owner | back to summary |
|---|---|
| public Thread owner() Returns the owner of this flock.
| |
| scopedValueBindings | back to summary |
|---|---|
| private ScopedValueContainer. | |
| shutdown | back to summary |
|---|---|
| public void shutdown() Shutdown this flock so that no new threads can be started, existing threads in the flock will continue to run. This method is a no-op if the flock is already shutdown or closed. | |
| start | back to summary |
|---|---|
| public Thread start(Thread thread) Starts the given unstarted thread in this flock. The thread is started with the scoped value bindings that were captured when opening the flock. The bindings must match the current thread's bindings. This method may only be invoked by the flock owner or threads contained in the flock.
| |
| threadCount | back to summary |
|---|---|
| private long threadCount() | |
| threads | back to summary |
|---|---|
| public Stream Returns a stream of the threads in this flock. The elements of the stream are threads that were started in this flock but have not terminated. The stream will reflect the set of threads in the flock at some point at or since the creation of the stream. It may or may not reflect changes to the set of threads subsequent to creation of the stream. | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non-
| |
| wakeup | back to summary |
|---|---|
| public void wakeup() Causes the call to If the owner is blocked in | |
| Modifier and Type | Field and Description |
|---|---|
| private boolean | |
| private final ThreadFlock | |
| private volatile Object |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv void | |
| public String | name()
Overrides jdk. Return the name of this container, may be null. |
| public void | onExit(Thread thread)
Overrides jdk. Invoked by |
| public void | onStart(Thread thread)
Overrides jdk. Invoked by |
| public ThreadFlock. | push()
Overrides jdk. Pushes this scope onto the current thread's scope stack. |
| public ScopedValueContainer. | scopedValueBindings()
Overrides jdk. The scoped values captured when the thread container was created. |
| public long | threadCount()
Overrides jdk. Return a count of the number of threads in this container. |
| public Stream | threads()
Implements abstract jdk. Returns a stream of the live threads in this container. |
| protected boolean | tryClose()
Overrides jdk. Invoked when an enclosing scope is closing. |
| closing | back to summary |
|---|---|
| private boolean closing | |
| flock | back to summary |
|---|---|
| private final ThreadFlock flock | |
| key | back to summary |
|---|---|
| private volatile Object key | |
| ThreadContainerImpl | back to summary |
|---|---|
| pack-priv ThreadContainerImpl(ThreadFlock flock) | |
| close | back to summary |
|---|---|
| pack-priv void close() Invoked by ThreadFlock.close when closing the flock. This method pops the container from the current thread's scope stack. | |
| name | back to summary |
|---|---|
| public String name() Overrides jdk. Doc from jdk. Return the name of this container, may be null.
| |
| onExit | back to summary |
|---|---|
| public void onExit(Thread thread) Overrides jdk. Doc from jdk. Invoked by
| |
| onStart | back to summary |
|---|---|
| public void onStart(Thread thread) Overrides jdk. Doc from jdk. Invoked by
| |
| push | back to summary |
|---|---|
| public ThreadFlock. Overrides jdk. Doc from jdk. Pushes this scope onto the current thread's scope stack.
| |
| scopedValueBindings | back to summary |
|---|---|
| public ScopedValueContainer. Overrides jdk. Doc from jdk. The scoped values captured when the thread container was created.
| |
| threadCount | back to summary |
|---|---|
| public long threadCount() Overrides jdk. Doc from jdk. Return a count of the number of threads in this container.
| |
| threads | back to summary |
|---|---|
| public Stream Implements abstract jdk. Doc from jdk. Returns a stream of the live threads in this container.
| |
| tryClose | back to summary |
|---|---|
| protected boolean tryClose() Overrides jdk. Invoked when an enclosing scope is closing. Invokes ThreadFlock.close to close the flock. This method does not pop the container from the current thread's scope stack.
| |