public abstract class Strand
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
static class |
Strand.State
A strand's running state
|
static interface |
Strand.UncaughtExceptionHandler
Interface for handlers invoked when a
Strand abruptly terminates due to an uncaught exception. |
| Modifier and Type | Field and Description |
|---|---|
protected static java.lang.ThreadLocal<Strand> |
currentStrand |
static int |
MAX_PRIORITY
The maximum priority that a strand can have.
|
static int |
MIN_PRIORITY
The minimum priority that a strand can have.
|
static int |
NORM_PRIORITY
The default priority that is assigned to a strand.
|
| Constructor and Description |
|---|
Strand() |
| Modifier and Type | Method and Description |
|---|---|
static Strand |
clone(Strand strand,
SuspendableCallable<?> target) |
static Strand |
clone(Strand strand,
SuspendableRunnable target) |
static Strand |
currentStrand()
Returns the current strand.
|
static void |
dumpStack()
Prints a stack trace of the current strand to the standard error stream.
|
static boolean |
equals(java.lang.Object strand1,
java.lang.Object strand2)
Tests whether two strands represent the same fiber or thread.
|
abstract java.lang.Object |
get() |
abstract java.lang.Object |
get(long timeout,
java.util.concurrent.TimeUnit unit) |
abstract java.lang.Object |
getBlocker()
Returns the blocker object supplied to the most recent
invocation of a
park method that has not yet unblocked, or null
if not blocked. |
abstract long |
getId()
Returns the strand's id.
|
abstract java.lang.InterruptedException |
getInterruptStack()
Returns an
InterruptedException that was created when the interrupt() method was called, and can be used
to retrieve the stack trace of the strand that interrupted this strand. |
abstract java.lang.String |
getName()
Returns the strand's name.
|
abstract int |
getPriority()
Returns this strand's priority.
|
abstract java.lang.StackTraceElement[] |
getStackTrace()
Returns an array of stack trace elements representing the stack dump
of this strand.
|
abstract Strand.State |
getState()
Returns the strand's current running state.
|
abstract Strand.UncaughtExceptionHandler |
getUncaughtExceptionHandler()
Returns the handler invoked when this strand abruptly terminates
due to an uncaught exception.
|
abstract java.lang.Object |
getUnderlying()
Returns the underlying object of this strand, namely a
Thread or a Fiber. |
abstract void |
interrupt()
Interrupts this strand.
|
static boolean |
interrupted()
Tests whether the current strand has been interrupted.
|
abstract boolean |
isAlive()
Tests whether this strand is alive, namely it has been started but not yet terminated.
|
static boolean |
isCurrentFiber()
Tests whether this function is called within a fiber.
|
boolean |
isDone() |
abstract boolean |
isFiber()
Tests whether this strand is a fiber.
|
abstract boolean |
isInterrupted()
Tests whether this strand has been interrupted.
|
abstract boolean |
isTerminated()
Tests whether this strand has terminated.
|
abstract void |
join()
Awaits the termination of this strand.
|
abstract void |
join(long timeout,
java.util.concurrent.TimeUnit unit)
Awaits the termination of this strand, at most for the timeout duration specified.
|
static void |
join(java.lang.Object strand)
Awaits the termination of a given strand.
|
static void |
join(java.lang.Object strand,
long timeout,
java.util.concurrent.TimeUnit unit)
Awaits the termination of a given strand, at most for the timeout duration specified.
|
static Strand |
of(Fiber fiber)
Returns a strand representing the given fiber.
|
static Strand |
of(java.lang.Object owner) |
static Strand |
of(java.lang.Thread thread)
Returns a strand representing the given thread.
|
static void |
park()
Disables the current strand for scheduling purposes unless the
permit is available.
|
static void |
park(java.lang.Object blocker)
Disables the current strand for scheduling purposes unless the
permit is available.
|
static void |
parkAndUnpark(Strand other) |
static void |
parkAndUnpark(Strand other,
java.lang.Object blocker) |
static void |
parkNanos(long nanos)
Disables the current strand for thread scheduling purposes, for up to
the specified waiting time, unless the permit is available.
|
static void |
parkNanos(java.lang.Object blocker,
long nanos)
Disables the current strand for thread scheduling purposes, for up to
the specified waiting time, unless the permit is available.
|
static void |
parkUntil(java.lang.Object blocker,
long deadline)
Disables the current strand for scheduling purposes, until
the specified deadline, unless the permit is available.
|
static void |
printStackTrace(java.lang.StackTraceElement[] trace,
java.io.PrintStream out)
This utility method prints a stack-trace into a
PrintStream |
static void |
printStackTrace(java.lang.StackTraceElement[] trace,
java.io.PrintWriter out)
This utility method prints a stack-trace into a
PrintWriter |
abstract Strand |
setName(java.lang.String name)
Sets this strand's name.
|
abstract Strand |
setPriority(int newPriority)
Attempts to change the priority of this strand.
|
abstract void |
setUncaughtExceptionHandler(Strand.UncaughtExceptionHandler eh)
Set the handler invoked when this strand abruptly terminates
due to an uncaught exception.
|
static void |
sleep(long millis)
Causes the currently executing strand to sleep (temporarily cease
execution) for the specified number of milliseconds, subject to
the precision and accuracy of system timers and schedulers.
|
static void |
sleep(long millis,
int nanos)
Causes the currently executing strand to sleep (temporarily cease
execution) for the specified number of milliseconds plus the specified
number of nanoseconds, subject to the precision and accuracy of system
timers and schedulers.
|
static void |
sleep(long duration,
java.util.concurrent.TimeUnit unit)
Causes the currently executing strand to sleep (temporarily cease
execution) for the specified duration, subject to
the precision and accuracy of system timers and schedulers.
|
abstract Strand |
start()
Starts the strand.
|
static java.lang.Runnable |
toRunnable(SuspendableCallable<?> callable)
A utility method that converts a
SuspendableCallable to a Runnable so that it could run
as the target of a thread. |
static java.lang.Runnable |
toRunnable(SuspendableRunnable runnable)
A utility method that converts a
SuspendableRunnable to a Runnable so that it could run
as the target of a thread. |
static java.lang.String |
toString(java.lang.StackTraceElement[] trace)
This utility method turns a stack-trace into a human readable, multi-line string.
|
abstract void |
unpark()
Makes available the permit for this strand, if it
was not already available.
|
abstract void |
unpark(java.lang.Object unblocker)
Makes available the permit for this strand, if it
was not already available.
|
static void |
unpark(Strand strand)
Makes available the permit for the given strand, if it
was not already available.
|
static void |
unpark(Strand strand,
java.lang.Object unblocker)
Makes available the permit for the given strand, if it
was not already available.
|
static void |
unpark(java.lang.Thread strand)
Makes available the permit for the given strand, if it
was not already available.
|
static java.lang.Object |
unwrapSuspendable(java.lang.Runnable r)
Returns the
SuspendableCallable or SuspendableRunnable, wrapped by the given Runnable
by toRunnable. |
static void |
yield()
A hint to the scheduler that the current strand is willing to yield
its current use of a processor.
|
static void |
yieldAndUnpark(Strand other) |
static void |
yieldAndUnpark(Strand other,
java.lang.Object blocker) |
public static final int MIN_PRIORITY
public static final int NORM_PRIORITY
public static final int MAX_PRIORITY
protected static java.lang.ThreadLocal<Strand> currentStrand
public static Strand of(java.lang.Object owner)
public static Strand of(java.lang.Thread thread)
public static Strand of(Fiber fiber)
Fiber extends Fiber.public abstract boolean isFiber()
true iff this strand is a fiber.public abstract java.lang.Object getUnderlying()
Thread or a Fiber.public abstract java.lang.String getName()
null.public abstract Strand setName(java.lang.String name)
name - the new namethispublic abstract Strand setPriority(int newPriority)
newPriority and the maximum permitted
priority of the strand's thread group, if the strand is a thread.
The strand priority's semantics - or even if it is ignored completely -
is entirely up to the strand's scheduler, be it the OS kernel in the case of
a thread, or the fiber scheduler, in the case of a fiber.newPriority - priority to set this strand tojava.lang.IllegalArgumentException - If the priority is not in the
range MIN_PRIORITY to MAX_PRIORITYgetPriority(),
MAX_PRIORITY,
MIN_PRIORITYpublic abstract int getPriority()
setPriority(int)public abstract boolean isAlive()
public abstract boolean isTerminated()
public abstract Strand start()
thisjava.lang.IllegalThreadStateException - if the strand has already been startedpublic abstract void join()
throws java.util.concurrent.ExecutionException,
java.lang.InterruptedException
java.util.concurrent.ExecutionException - if this strand has terminated as a result of an uncaught exception
(which will be the cause of the thrown ExecutionException.java.lang.InterruptedExceptionpublic abstract void join(long timeout,
java.util.concurrent.TimeUnit unit)
throws java.util.concurrent.ExecutionException,
java.lang.InterruptedException,
java.util.concurrent.TimeoutException
timeout - the maximum duration to wait for the strand to terminate in the time unit specified by unit.unit - the time unit of timeout.java.util.concurrent.TimeoutException - if this strand did not terminate by the time the timeout has elapsed.java.util.concurrent.ExecutionException - if this strand has terminated as a result of an uncaught exception
(which will be the cause of the thrown ExecutionException.java.lang.InterruptedExceptionpublic abstract java.lang.Object get()
throws java.util.concurrent.ExecutionException,
java.lang.InterruptedException
java.util.concurrent.ExecutionExceptionjava.lang.InterruptedExceptionpublic abstract java.lang.Object get(long timeout,
java.util.concurrent.TimeUnit unit)
throws java.util.concurrent.ExecutionException,
java.lang.InterruptedException,
java.util.concurrent.TimeoutException
java.util.concurrent.ExecutionExceptionjava.lang.InterruptedExceptionjava.util.concurrent.TimeoutExceptionpublic boolean isDone()
public abstract void interrupt()
InterruptedException.
Otherwise, the strand may test its interrupted status with the interrupted() or isInterrupted() method.public abstract boolean isInterrupted()
true if the strand has been interrupted; false otherwise.interrupt(),
interrupted()public abstract java.lang.InterruptedException getInterruptStack()
InterruptedException that was created when the interrupt() method was called, and can be used
to retrieve the stack trace of the strand that interrupted this strand.
This method is only intended to assist in debugging.
This method may return null if this information is not available. The current implementation always returns null
if this strand is a thread.public abstract void unpark()
public abstract void unpark(java.lang.Object unblocker)
park() then it will unblock. Otherwise, its next call
to park() is guaranteed not to block. This operation
is not guaranteed to have any effect at all if the given
strand has not been started.unblocker - the synchronization object responsible for this strand unparkingpublic abstract java.lang.Object getBlocker()
park method that has not yet unblocked, or null
if not blocked. The value returned is just a momentary
snapshot -- the thread may have since unblocked or blocked on a
different blocker object.public abstract Strand.State getState()
public abstract java.lang.StackTraceElement[] getStackTrace()
Some virtual machines may, under some circumstances, omit one or more stack frames from the stack trace. In the extreme case, a virtual machine that has no stack trace information concerning this strand is permitted to return a zero-length array from this method.
StackTraceElements, each represents one stack frame.public abstract long getId()
public static Strand currentStrand()
public static boolean isCurrentFiber()
Fiber.currentFiber() != null.true iff the code that called this method is executing in a fiber.public static boolean interrupted()
false (unless the current strand were
interrupted again, after the first call had cleared its interrupted
status and before the second call had examined it).true if the current thread has been interrupted; false otherwise.interrupt(),
isInterrupted()public static void join(java.lang.Object strand)
throws java.util.concurrent.ExecutionException,
java.lang.InterruptedException
strand - the strand to join. May be an object of type Strand, Fiber or Thread.java.util.concurrent.ExecutionException - if this strand has terminated as a result of an uncaught exception
(which will be the cause of the thrown ExecutionException.java.lang.InterruptedExceptionpublic static void join(java.lang.Object strand,
long timeout,
java.util.concurrent.TimeUnit unit)
throws java.util.concurrent.ExecutionException,
java.lang.InterruptedException,
java.util.concurrent.TimeoutException
strand - the strand to join. May be an object of type Strand, Fiber or Thread.timeout - the maximum duration to wait for the strand to terminate in the time unit specified by unit.unit - the time unit of timeout.java.util.concurrent.TimeoutException - if this strand did not terminate by the time the timeout has elapsed.java.util.concurrent.ExecutionException - if this strand has terminated as a result of an uncaught exception
(which will be the cause of the thrown ExecutionException.java.lang.InterruptedExceptionpublic static void yield()
throws SuspendExecution
Yield is a heuristic attempt to improve relative progression between strands that would otherwise over-utilise a CPU. Its use should be combined with detailed profiling and benchmarking to ensure that it actually has the desired effect.
SuspendExecutionpublic static void sleep(long millis)
throws SuspendExecution,
java.lang.InterruptedException
millis - the length of time to sleep in millisecondsjava.lang.IllegalArgumentException - if the value of millis is negativejava.lang.InterruptedException - if any strand has interrupted the current strand. The
interrupted status of the current strand is
cleared when this exception is thrown.SuspendExecutionpublic static void sleep(long millis,
int nanos)
throws SuspendExecution,
java.lang.InterruptedException
millis - the length of time to sleep in millisecondsnanos - 0-999999 additional nanoseconds to sleepjava.lang.IllegalArgumentException - if the value of millis is negative,
or the value of nanos is not in the range 0-999999java.lang.InterruptedException - if any strand has interrupted the current strand. The
interrupted status of the current strand is
cleared when this exception is thrown.SuspendExecutionpublic static void sleep(long duration,
java.util.concurrent.TimeUnit unit)
throws SuspendExecution,
java.lang.InterruptedException
duration - the length of time to sleep in the time unit specified by unit.unit - the time unit of duration.java.lang.InterruptedException - if any strand has interrupted the current strand. The
interrupted status of the current strand is
cleared when this exception is thrown.SuspendExecutionpublic static void park()
throws SuspendExecution
If the permit is available then it is consumed and the call returns immediately; otherwise the current strand becomes disabled for scheduling purposes and lies dormant until one of three things happens:
unpark with the
current strand as the target; or
interrupts
the current strand; or
This method does not report which of these caused the method to return. Callers should re-check the conditions which caused the strand to park in the first place. Callers may also determine, for example, the interrupt status of the strand upon return.
SuspendExecutionpublic static void park(java.lang.Object blocker)
throws SuspendExecution
If the permit is available then it is consumed and the call returns immediately; otherwise the current strand becomes disabled for scheduling purposes and lies dormant until one of three things happens:
unpark with the
current strand as the target; or
interrupts
the current strand; or
This method does not report which of these caused the method to return. Callers should re-check the conditions which caused the strand to park in the first place. Callers may also determine, for example, the interrupt status of the strand upon return.
blocker - the synchronization object responsible for this strand parkingSuspendExecutionpublic static void parkAndUnpark(Strand other, java.lang.Object blocker) throws SuspendExecution
SuspendExecutionpublic static void parkAndUnpark(Strand other) throws SuspendExecution
SuspendExecutionpublic static void yieldAndUnpark(Strand other, java.lang.Object blocker) throws SuspendExecution
SuspendExecutionpublic static void yieldAndUnpark(Strand other) throws SuspendExecution
SuspendExecutionpublic static void parkNanos(long nanos)
throws SuspendExecution
If the permit is available then it is consumed and the call returns immediately; otherwise the current strand becomes disabled for scheduling purposes and lies dormant until one of four things happens:
unpark with the
current strand as the target; or
interrupts
the current strand; or
This method does not report which of these caused the method to return. Callers should re-check the conditions which caused the strand to park in the first place. Callers may also determine, for example, the interrupt status of the strand, or the elapsed time upon return.
nanos - the maximum number of nanoseconds to waitSuspendExecutionpublic static void parkNanos(java.lang.Object blocker,
long nanos)
throws SuspendExecution
If the permit is available then it is consumed and the call returns immediately; otherwise the current strand becomes disabled for scheduling purposes and lies dormant until one of four things happens:
unpark with the
current strand as the target; or
interrupts
the current strand; or
This method does not report which of these caused the method to return. Callers should re-check the conditions which caused the strand to park in the first place. Callers may also determine, for example, the interrupt status of the strand, or the elapsed time upon return.
blocker - the synchronization object responsible for this strand parkingnanos - the maximum number of nanoseconds to waitSuspendExecutionpublic static void parkUntil(java.lang.Object blocker,
long deadline)
throws SuspendExecution
If the permit is available then it is consumed and the call returns immediately; otherwise the current strand becomes disabled for scheduling purposes and lies dormant until one of four things happens:
unpark with the
current strand as the target; or
interrupts the
current strand; or
This method does not report which of these caused the method to return. Callers should re-check the conditions which caused the strand to park in the first place. Callers may also determine, for example, the interrupt status of the strand, or the current time upon return.
blocker - the synchronization object responsible for this strand parkingdeadline - the absolute time, in milliseconds from the Epoch, to wait untilSuspendExecutionpublic static void unpark(Strand strand)
park then it will unblock. Otherwise, its next call
to park is guaranteed not to block. This operation
is not guaranteed to have any effect at all if the given
strand has not been started.strand - the strand to unpark, or null, in which case this operation has no effectpublic static void unpark(Strand strand, java.lang.Object unblocker)
park then it will unblock. Otherwise, its next call
to park is guaranteed not to block. This operation
is not guaranteed to have any effect at all if the given
strand has not been started.strand - the strand to unpark, or null, in which case this operation has no effectunblocker - the synchronization object responsible for the strand unparkingpublic static void unpark(java.lang.Thread strand)
park then it will unblock. Otherwise, its next call
to park is guaranteed not to block. This operation
is not guaranteed to have any effect at all if the given
strand has not been started.strand - the strand to unpark, or null, in which case this operation has no effectpublic static void dumpStack()
public abstract void setUncaughtExceptionHandler(Strand.UncaughtExceptionHandler eh)
A strand can take full control of how it responds to uncaught exceptions by having its uncaught exception handler explicitly set.
eh - the object to use as this strand's uncaught exception handler.
If null then this strand has no explicit handler.public abstract Strand.UncaughtExceptionHandler getUncaughtExceptionHandler()
public static boolean equals(java.lang.Object strand1,
java.lang.Object strand2)
strand1 - May be an object of type Strand, Fiber or Thread.strand2 - May be an object of type Strand, Fiber or Thread.true if the two strands represent the same fiber or the same thread; false otherwise.public static Strand clone(Strand strand, SuspendableCallable<?> target)
public static Strand clone(Strand strand, SuspendableRunnable target)
public static java.lang.Runnable toRunnable(SuspendableRunnable runnable)
SuspendableRunnable to a Runnable so that it could run
as the target of a thread.public static java.lang.Runnable toRunnable(SuspendableCallable<?> callable)
SuspendableCallable to a Runnable so that it could run
as the target of a thread. The return value of the callable is ignored.public static java.lang.Object unwrapSuspendable(java.lang.Runnable r)
SuspendableCallable or SuspendableRunnable, wrapped by the given Runnable
by toRunnable.public static java.lang.String toString(java.lang.StackTraceElement[] trace)
trace - a stack trace (such as returned from getStackTrace().public static void printStackTrace(java.lang.StackTraceElement[] trace,
java.io.PrintStream out)
PrintStreamtrace - a stack trace (such as returned from getStackTrace().out - the PrintStream into which the stack trace will be printed.public static void printStackTrace(java.lang.StackTraceElement[] trace,
java.io.PrintWriter out)
PrintWritertrace - a stack trace (such as returned from getStackTrace().out - the PrintWriter into which the stack trace will be printed.