Class Fiber<V>
- java.lang.Object
-
- co.paralleluniverse.strands.Strand
-
- co.paralleluniverse.fibers.Fiber<V>
-
- Type Parameters:
V
- The type of the fiber's result value. Should be set toVoid
if no value is to be returned by the fiber.
- All Implemented Interfaces:
Joinable<V>
,java.io.Serializable
,java.util.concurrent.Future<V>
public class Fiber<V> extends Strand implements Joinable<V>, java.io.Serializable, java.util.concurrent.Future<V>
A lightweight thread.There are two ways to create a new fiber: either subclass the
Fiber
class and override therun
method, or pass the code to be executed in the fiber as thetarget
parameter to the constructor. All in all, the Fiber API resembles theThread
class in many ways.A fiber runs inside a ForkJoinPool.
A Fiber can be serialized if it's not running and all involved classes and data types are also
Serializable
.A new Fiber occupies under 400 bytes of memory (when using the default stack size, and compressed OOPs are turned on, as they are by default).
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class co.paralleluniverse.strands.Strand
Strand.State, Strand.UncaughtExceptionHandler
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_STACK_SIZE
protected static co.paralleluniverse.common.monitoring.FlightRecorder
flightRecorder
-
Fields inherited from class co.paralleluniverse.strands.Strand
currentStrand, MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
-
-
Constructor Summary
Constructors Constructor Description Fiber()
Creates a new child Fiber subclassing the Fiber class and overriding therun
method.Fiber(Fiber fiber, FiberScheduler scheduler, SuspendableCallable<V> target)
Fiber(Fiber fiber, FiberScheduler scheduler, SuspendableRunnable target)
Fiber(Fiber fiber, SuspendableCallable<V> target)
Fiber(Fiber fiber, SuspendableRunnable target)
Fiber(FiberScheduler scheduler)
Creates a new Fiber subclassing the Fiber class and overriding therun
method.Fiber(FiberScheduler scheduler, SuspendableCallable<V> target)
Creates a new Fiber from the givenSuspendableCallable
.Fiber(FiberScheduler scheduler, SuspendableRunnable target)
Creates a new Fiber from the given SuspendableRunnable.Fiber(SuspendableCallable<V> target)
Creates a new child Fiber from the givenSuspendableCallable
.Fiber(SuspendableRunnable target)
Creates a new child Fiber from the givenSuspendableRunnable
.Fiber(java.lang.String name)
Creates a new child Fiber subclassing the Fiber class and overriding therun
method.Fiber(java.lang.String name, int stackSize)
Creates a new child Fiber subclassing the Fiber class and overriding therun
method.Fiber(java.lang.String name, int stackSize, SuspendableCallable<V> target)
Creates a new fiber from the givenSuspendableCallable
scheduled in thedefault fiber scheduler
Fiber(java.lang.String name, int stackSize, SuspendableRunnable target)
Creates a new child Fiber from the givenSuspendableRunnable
.Fiber(java.lang.String name, FiberScheduler scheduler)
Creates a new Fiber subclassing the Fiber class and overriding therun
method.Fiber(java.lang.String name, FiberScheduler scheduler, int stackSize)
Creates a new Fiber subclassing the Fiber class and overriding therun
method.Fiber(java.lang.String name, FiberScheduler scheduler, int stackSize, SuspendableCallable<V> target)
Creates a new fiber from the givenSuspendableCallable
.Fiber(java.lang.String name, FiberScheduler scheduler, int stackSize, SuspendableRunnable target)
Creates a new Fiber from the givenSuspendableRunnable
.Fiber(java.lang.String name, FiberScheduler scheduler, SuspendableCallable<V> target)
Creates a new Fiber from the givenSuspendableCallable
.Fiber(java.lang.String name, FiberScheduler scheduler, SuspendableRunnable target)
Creates a new Fiber from the givenSuspendableRunnable
.Fiber(java.lang.String name, SuspendableCallable<V> target)
Creates a new child Fiber from the givenSuspendableCallable
.Fiber(java.lang.String name, SuspendableRunnable target)
Creates a new child Fiber from the givenSuspendableRunnable
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description boolean
cancel(boolean mayInterruptIfRunning)
static Fiber
currentFiber()
Returns the active Fiber on this thread or NULL if no Fiber is running.static void
dumpStack()
boolean
equals(java.lang.Object obj)
V
get()
V
get(long timeout, java.util.concurrent.TimeUnit unit)
java.lang.Object
getBlocker()
Returns the blocker object supplied to the most recent invocation of apark
method that has not yet unblocked, or null if not blocked.static long
getCurrentRun()
static Strand.UncaughtExceptionHandler
getDefaultUncaughtExceptionHandler()
Returns the default handler invoked when a fiber abruptly terminates due to an uncaught exception.static co.paralleluniverse.io.serialization.ByteArraySerializer
getFiberSerializer()
Returns aByteArraySerializer
capable of serializing an object graph containing fibers.static co.paralleluniverse.io.serialization.ByteArraySerializer
getFiberSerializer(boolean includeThreadLocals)
Returns aByteArraySerializer
capable of serializing an object graph containing fibers.long
getId()
Returns the strand's id.java.lang.InterruptedException
getInterruptStack()
Returns anInterruptedException
that was created when theStrand.interrupt()
method was called, and can be used to retrieve the stack trace of the strand that interrupted this strand.co.paralleluniverse.fibers.FibersMonitor
getMonitor()
java.lang.String
getName()
Returns the strand's name.boolean
getNoLocals()
Deprecated.int
getPriority()
Returns this fiber's priority.FiberScheduler
getScheduler()
java.lang.StackTraceElement[]
getStackTrace()
Returns an array of stack trace elements representing the stack dump of this strand.Strand.State
getState()
Returns the strand's current running state.SuspendableCallable<V>
getTarget()
java.lang.Object
getTask()
Strand.UncaughtExceptionHandler
getUncaughtExceptionHandler()
Returns the handler invoked when this fiber abruptly terminates due to an uncaught exception.java.lang.Object
getUnderlying()
Returns the underlying object of this strand, namely aThread
or aFiber
.int
hashCode()
Fiber
inheritThreadLocals()
Causes the current strand'sthread-locals
to be inherited by this fiber.void
interrupt()
Interrupts this strand.static boolean
interrupted()
boolean
isAlive()
Tests whether this strand is alive, namely it has been started but not yet terminated.boolean
isCancelled()
static boolean
isCurrentFiber()
Tests whether current code is executing in a fiber.boolean
isDone()
boolean
isFiber()
Tests whether this strand is a fiber.boolean
isInterrupted()
Tests whether this strand has been interrupted.protected boolean
isRecordingLevel(int level)
boolean
isTerminated()
Tests whether this strand has terminated.void
join()
Awaits the termination of this strand.void
join(long timeout, java.util.concurrent.TimeUnit unit)
Awaits the termination of this strand, at most for the timeout duration specified.Fiber<V>
joinNoSuspend()
Fiber<V>
joinNoSuspend(long timeout, java.util.concurrent.TimeUnit unit)
protected void
onCompletion()
protected void
onException(java.lang.Throwable t)
protected void
onParked()
protected void
onResume()
static void
park()
static void
park(long timeout, java.util.concurrent.TimeUnit unit)
static void
park(java.lang.Object blocker)
static boolean
park(java.lang.Object blocker, long timeout, java.util.concurrent.TimeUnit unit)
static void
parkAndCustomSerialize(CustomFiberWriter writer)
Parks the fiber and allows the given callback to serialize it, optimized for use cases where the callback object has a custom way to obtain the required serializer (e.g.static void
parkAndSerialize(FiberWriter writer)
Parks the fiber and allows the given callback to serialize it.static void
parkAndUnpark(Fiber other)
static void
parkAndUnpark(Fiber other, java.lang.Object blocker)
protected void
record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format)
protected void
record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object arg1)
protected void
record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object... args)
protected void
record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object arg1, java.lang.Object arg2)
protected void
record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3)
protected void
record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4)
protected void
record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4, java.lang.Object arg5)
protected void
record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4, java.lang.Object arg5, java.lang.Object arg6)
protected void
record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4, java.lang.Object arg5, java.lang.Object arg6, java.lang.Object arg7)
protected V
run()
static void
setDefaultUncaughtExceptionHandler(Strand.UncaughtExceptionHandler eh)
Set the default handler invoked when a fiber abruptly terminates due to an uncaught exception, and no other handler has been defined for that fiber.Fiber<V>
setName(java.lang.String name)
Sets this strand's name.Fiber
setNoLocals(boolean value)
Deprecated.Fiber
setPriority(int newPriority)
Sets the priority of this fiber.void
setUncaughtExceptionHandler(Strand.UncaughtExceptionHandler eh)
Set the handler invoked when this fiber abruptly terminates due to an uncaught exception.protected boolean
shouldPreempt(int type)
static void
sleep(long millis)
static void
sleep(long millis, int nanos)
static void
sleep(long duration, java.util.concurrent.TimeUnit unit)
Fiber<V>
start()
Starts the strand.java.lang.String
toString()
void
unpark()
Makes available the permit for this fiber, if it was not already available.void
unpark(java.lang.Object unblocker)
Makes available the permit for this strand, if it was not already available.static <V> Fiber<V>
unparkDeserialized(Fiber<V> f, FiberScheduler scheduler)
Unparks a fiber that's been deserialized (with the help ofgetFiberSerializer()
static <V> Fiber<V>
unparkSerialized(byte[] serFiber, FiberScheduler scheduler)
Deserializes a fiber from the given byte array and unparks it.static void
yield()
static void
yieldAndUnpark(Fiber other)
static void
yieldAndUnpark(Fiber other, java.lang.Object blocker)
-
Methods inherited from class co.paralleluniverse.strands.Strand
clone, clone, currentStrand, equals, join, join, of, of, of, parkAndUnpark, parkAndUnpark, parkNanos, parkNanos, parkUntil, printStackTrace, printStackTrace, toRunnable, toRunnable, toString, unpark, unpark, unpark, unwrapSuspendable, yieldAndUnpark, yieldAndUnpark
-
-
-
-
Field Detail
-
DEFAULT_STACK_SIZE
public static final int DEFAULT_STACK_SIZE
- See Also:
- Constant Field Values
-
flightRecorder
protected static final co.paralleluniverse.common.monitoring.FlightRecorder flightRecorder
-
-
Constructor Detail
-
Fiber
public Fiber(java.lang.String name, FiberScheduler scheduler, int stackSize, SuspendableCallable<V> target)
Creates a new fiber from the givenSuspendableCallable
.- Parameters:
name
- The name of the fiber (may benull
)scheduler
- TheFiberScheduler
that will schedule the fiber.stackSize
- the initial size of the data stack.target
- theSuspendableCallable
for the fiber. Maynull
if therun
method is overriden.
-
Fiber
public Fiber(java.lang.String name, int stackSize, SuspendableCallable<V> target)
Creates a new fiber from the givenSuspendableCallable
scheduled in thedefault fiber scheduler
- Parameters:
name
- The name of the fiber (may benull
)stackSize
- the initial size of the data stack.target
- theSuspendableCallable
for the fiber. Maynull
if therun
method is overriden.
-
Fiber
public Fiber(java.lang.String name, FiberScheduler scheduler, SuspendableCallable<V> target)
Creates a new Fiber from the givenSuspendableCallable
. The new fiber uses the default initial stack size.- Parameters:
name
- The name of the fiber (may be null)scheduler
- The scheduler pool in which the fiber should run.target
- the SuspendableCallable for the Fiber.- Throws:
java.lang.NullPointerException
- when proto is nulljava.lang.IllegalArgumentException
- when stackSize is <= 0
-
Fiber
public Fiber(FiberScheduler scheduler, SuspendableCallable<V> target)
Creates a new Fiber from the givenSuspendableCallable
. The new fiber has no name, and uses the default initial stack size.- Parameters:
scheduler
- The scheduler pool in which the fiber should run.target
- the SuspendableRunnable for the Fiber.- Throws:
java.lang.NullPointerException
- when proto is nulljava.lang.IllegalArgumentException
- when stackSize is <= 0
-
Fiber
public Fiber(java.lang.String name, FiberScheduler scheduler, int stackSize, SuspendableRunnable target)
Creates a new Fiber from the givenSuspendableRunnable
.- Parameters:
name
- The name of the fiber (may be null)scheduler
- The scheduler pool in which the fiber should run.stackSize
- the initial size of the data stack.target
- the SuspendableRunnable for the Fiber.- Throws:
java.lang.NullPointerException
- when proto is nulljava.lang.IllegalArgumentException
- when stackSize is <= 0
-
Fiber
public Fiber(java.lang.String name, FiberScheduler scheduler, SuspendableRunnable target)
Creates a new Fiber from the givenSuspendableRunnable
. The new fiber uses the default initial stack size.- Parameters:
name
- The name of the fiber (may be null)scheduler
- The scheduler pool in which the fiber should run.target
- the SuspendableRunnable for the Fiber.- Throws:
java.lang.NullPointerException
- when proto is nulljava.lang.IllegalArgumentException
- when stackSize is <= 0
-
Fiber
public Fiber(FiberScheduler scheduler, SuspendableRunnable target)
Creates a new Fiber from the given SuspendableRunnable. The new fiber has no name, and uses the default initial stack size.- Parameters:
scheduler
- The scheduler pool in which the fiber should run.target
- the SuspendableRunnable for the Fiber.- Throws:
java.lang.NullPointerException
- when proto is nulljava.lang.IllegalArgumentException
- when stackSize is <= 0
-
Fiber
public Fiber(java.lang.String name, FiberScheduler scheduler, int stackSize)
Creates a new Fiber subclassing the Fiber class and overriding therun
method.- Parameters:
name
- The name of the fiber (may be null)scheduler
- The scheduler pool in which the fiber should run.stackSize
- the initial size of the data stack.- Throws:
java.lang.NullPointerException
- when proto is nulljava.lang.IllegalArgumentException
- when stackSize is <= 0
-
Fiber
public Fiber(java.lang.String name, FiberScheduler scheduler)
Creates a new Fiber subclassing the Fiber class and overriding therun
method. The new fiber uses the default initial stack size.- Parameters:
name
- The name of the fiber (may be null)scheduler
- The scheduler pool in which the fiber should run.- Throws:
java.lang.NullPointerException
- when proto is nulljava.lang.IllegalArgumentException
- when stackSize is <= 0
-
Fiber
public Fiber(FiberScheduler scheduler)
Creates a new Fiber subclassing the Fiber class and overriding therun
method. The new fiber has no name, and uses the default initial stack size.- Parameters:
scheduler
- The scheduler pool in which the fiber should run.- Throws:
java.lang.NullPointerException
- when proto is nulljava.lang.IllegalArgumentException
- when stackSize is <= 0
-
Fiber
public Fiber(java.lang.String name, SuspendableCallable<V> target)
Creates a new child Fiber from the givenSuspendableCallable
. This constructor may only be called from within another fiber. This fiber will use the same fork/join pool as the creating fiber. The new fiber uses the default initial stack size.- Parameters:
name
- The name of the fiber (may be null)target
- the SuspendableRunnable for the Fiber.- Throws:
java.lang.IllegalArgumentException
- when stackSize is <= 0
-
Fiber
public Fiber(SuspendableCallable<V> target)
Creates a new child Fiber from the givenSuspendableCallable
. This constructor may only be called from within another fiber. This fiber will use the same fork/join pool as the creating fiber. The new fiber has no name, and uses the default initial stack size.- Parameters:
target
- the SuspendableRunnable for the Fiber.- Throws:
java.lang.IllegalArgumentException
- when stackSize is <= 0
-
Fiber
public Fiber(java.lang.String name, int stackSize, SuspendableRunnable target)
Creates a new child Fiber from the givenSuspendableRunnable
. This constructor may only be called from within another fiber. This fiber will use the same fork/join pool as the creating fiber.- Parameters:
name
- The name of the fiber (may be null)stackSize
- the initial size of the data stack.target
- the SuspendableRunnable for the Fiber.- Throws:
java.lang.NullPointerException
- when proto is nulljava.lang.IllegalArgumentException
- when stackSize is <= 0
-
Fiber
public Fiber(java.lang.String name, SuspendableRunnable target)
Creates a new child Fiber from the givenSuspendableRunnable
. This constructor may only be called from within another fiber. This fiber will use the same fork/join pool as the creating fiber. The new fiber uses the default initial stack size.- Parameters:
name
- The name of the fiber (may be null)target
- the SuspendableRunnable for the Fiber.- Throws:
java.lang.NullPointerException
- when proto is nulljava.lang.IllegalArgumentException
- when stackSize is <= 0
-
Fiber
public Fiber(SuspendableRunnable target)
Creates a new child Fiber from the givenSuspendableRunnable
. This constructor may only be called from within another fiber. This fiber will use the same fork/join pool as the creating fiber. The new fiber has no name, and uses the default initial stack size.- Parameters:
target
- the SuspendableRunnable for the Fiber.- Throws:
java.lang.NullPointerException
- when proto is nulljava.lang.IllegalArgumentException
- when stackSize is <= 0
-
Fiber
public Fiber(java.lang.String name, int stackSize)
Creates a new child Fiber subclassing the Fiber class and overriding therun
method. This constructor may only be called from within another fiber. This fiber will use the same fork/join pool as the creating fiber.- Parameters:
name
- The name of the fiber (may be null)stackSize
- the initial size of the data stack.- Throws:
java.lang.NullPointerException
- when proto is nulljava.lang.IllegalArgumentException
- when stackSize is <= 0
-
Fiber
public Fiber(java.lang.String name)
Creates a new child Fiber subclassing the Fiber class and overriding therun
method. This constructor may only be called from within another fiber. This fiber will use the same fork/join pool as the creating fiber. The new fiber uses the default initial stack size.- Parameters:
name
- The name of the fiber (may be null)- Throws:
java.lang.NullPointerException
- when proto is nulljava.lang.IllegalArgumentException
- when stackSize is <= 0
-
Fiber
public Fiber()
Creates a new child Fiber subclassing the Fiber class and overriding therun
method. This constructor may only be called from within another fiber. This fiber will use the same fork/join pool as the creating fiber. The new fiber has no name, and uses the default initial stack size.- Throws:
java.lang.NullPointerException
- when proto is nulljava.lang.IllegalArgumentException
- when stackSize is <= 0
-
Fiber
public Fiber(Fiber fiber, SuspendableCallable<V> target)
-
Fiber
public Fiber(Fiber fiber, SuspendableRunnable target)
-
Fiber
public Fiber(Fiber fiber, FiberScheduler scheduler, SuspendableCallable<V> target)
-
Fiber
public Fiber(Fiber fiber, FiberScheduler scheduler, SuspendableRunnable target)
-
-
Method Detail
-
getTarget
public final SuspendableCallable<V> getTarget()
-
hashCode
public final int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public final boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
getName
public final java.lang.String getName()
Description copied from class:Strand
Returns the strand's name.
-
setName
public final Fiber<V> setName(java.lang.String name)
Description copied from class:Strand
Sets this strand's name. This method may only be called before the strand is started.
-
setPriority
public Fiber setPriority(int newPriority)
Sets the priority of this fiber. The fiber priority's semantics - or even if it is ignored completely - is entirely up to the fiber's scheduler. The default fiber scheduler completely ignores fiber priority.- Specified by:
setPriority
in classStrand
- Parameters:
newPriority
- priority to set this fiber to- Throws:
java.lang.IllegalArgumentException
- If the priority is not in the rangeMIN_PRIORITY
toMAX_PRIORITY
- See Also:
getPriority()
,Strand.MAX_PRIORITY
,Strand.MIN_PRIORITY
-
getPriority
public int getPriority()
Returns this fiber's priority.- Specified by:
getPriority
in classStrand
- Returns:
- this strand's priority.
- See Also:
setPriority(int)
-
getId
public long getId()
Description copied from class:Strand
Returns the strand's id. Id's are unique within a single JVM instance.
-
getTask
public java.lang.Object getTask()
-
getScheduler
public FiberScheduler getScheduler()
-
setNoLocals
@Deprecated public Fiber setNoLocals(boolean value)
Deprecated.
-
getNoLocals
@Deprecated public boolean getNoLocals()
Deprecated.
-
currentFiber
public static Fiber currentFiber()
Returns the active Fiber on this thread or NULL if no Fiber is running.- Returns:
- the active Fiber on this thread or NULL if no Fiber is running.
-
isCurrentFiber
public static boolean isCurrentFiber()
Tests whether current code is executing in a fiber. This method might be faster thanFiber.currentFiber() != null
.- Returns:
true
if called in a fiber;false
otherwise.
-
getCurrentRun
public static long getCurrentRun()
-
isFiber
public final boolean isFiber()
Description copied from class:Strand
Tests whether this strand is a fiber.
-
getUnderlying
public final java.lang.Object getUnderlying()
Description copied from class:Strand
Returns the underlying object of this strand, namely aThread
or aFiber
.- Specified by:
getUnderlying
in classStrand
-
park
public static boolean park(java.lang.Object blocker, long timeout, java.util.concurrent.TimeUnit unit) throws SuspendExecution
- Throws:
SuspendExecution
-
park
public static void park(java.lang.Object blocker) throws SuspendExecution
- Throws:
SuspendExecution
-
park
public static void park(long timeout, java.util.concurrent.TimeUnit unit) throws SuspendExecution
- Throws:
SuspendExecution
-
park
public static void park() throws SuspendExecution
- Throws:
SuspendExecution
-
yield
public static void yield() throws SuspendExecution
- Throws:
SuspendExecution
-
parkAndUnpark
public static void parkAndUnpark(Fiber other) throws SuspendExecution
- Throws:
SuspendExecution
-
parkAndUnpark
public static void parkAndUnpark(Fiber other, java.lang.Object blocker) throws SuspendExecution
- Throws:
SuspendExecution
-
yieldAndUnpark
public static void yieldAndUnpark(Fiber other, java.lang.Object blocker) throws SuspendExecution
- Throws:
SuspendExecution
-
yieldAndUnpark
public static void yieldAndUnpark(Fiber other) throws SuspendExecution
- Throws:
SuspendExecution
-
sleep
public static void sleep(long millis) throws java.lang.InterruptedException, SuspendExecution
- Throws:
java.lang.InterruptedException
SuspendExecution
-
sleep
public static void sleep(long millis, int nanos) throws java.lang.InterruptedException, SuspendExecution
- Throws:
java.lang.InterruptedException
SuspendExecution
-
sleep
public static void sleep(long duration, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, SuspendExecution
- Throws:
java.lang.InterruptedException
SuspendExecution
-
interrupted
public static boolean interrupted()
-
getMonitor
public co.paralleluniverse.fibers.FibersMonitor getMonitor()
-
run
protected V run() throws SuspendExecution, java.lang.InterruptedException
- Throws:
SuspendExecution
java.lang.InterruptedException
-
inheritThreadLocals
public Fiber inheritThreadLocals()
Causes the current strand'sthread-locals
to be inherited by this fiber. By default onlyInheritableThreadLocal
s are inherited.This method must be called before the fiber is started (i.e. before the
start
method is called. Otherwise, anIllegalStateException
is thrown.- Returns:
this
-
onParked
protected void onParked()
-
onResume
protected void onResume() throws SuspendExecution, java.lang.InterruptedException
- Throws:
SuspendExecution
java.lang.InterruptedException
-
shouldPreempt
protected boolean shouldPreempt(int type)
-
onCompletion
protected void onCompletion()
-
onException
protected void onException(java.lang.Throwable t)
-
interrupt
public final void interrupt()
Description copied from class:Strand
Interrupts this strand. If this strand is blocked, the blocking function will throw anInterruptedException
. Otherwise, the strand may test its interrupted status with theStrand.interrupted()
orStrand.isInterrupted()
method.
-
isInterrupted
public final boolean isInterrupted()
Description copied from class:Strand
Tests whether this strand has been interrupted.- Specified by:
isInterrupted
in classStrand
- Returns:
true
if the strand has been interrupted;false
otherwise.- See Also:
Strand.interrupt()
,Strand.interrupted()
-
getInterruptStack
public final java.lang.InterruptedException getInterruptStack()
Description copied from class:Strand
Returns anInterruptedException
that was created when theStrand.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 returnnull
if this information is not available. The current implementation always returnsnull
if this strand is a thread.- Specified by:
getInterruptStack
in classStrand
-
isAlive
public final boolean isAlive()
Description copied from class:Strand
Tests whether this strand is alive, namely it has been started but not yet terminated.
-
getState
public final Strand.State getState()
Description copied from class:Strand
Returns the strand's current running state.
-
isTerminated
public final boolean isTerminated()
Description copied from class:Strand
Tests whether this strand has terminated.- Specified by:
isTerminated
in classStrand
-
getBlocker
public final java.lang.Object getBlocker()
Description copied from class:Strand
Returns the blocker object supplied to the most recent invocation of apark
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.- Specified by:
getBlocker
in classStrand
- Returns:
- the blocker
-
unpark
public final void unpark()
Makes available the permit for this fiber, if it was not already available. If the fiber was blocked onpark
then it will unblock. Otherwise, its next call topark
is guaranteed not to block.
-
unpark
public final void unpark(java.lang.Object unblocker)
Description copied from class:Strand
Makes available the permit for this strand, if it was not already available. If this strand was blocked onStrand.park()
then it will unblock. Otherwise, its next call toStrand.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.
-
join
@Suspendable public final void join() throws java.util.concurrent.ExecutionException, java.lang.InterruptedException
Description copied from class:Strand
Awaits the termination of this strand. This method blocks until this strand terminates.
-
join
@Suspendable public final void join(long timeout, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.ExecutionException, java.lang.InterruptedException, java.util.concurrent.TimeoutException
Description copied from class:Strand
Awaits the termination of this strand, at most for the timeout duration specified. This method blocks until this strand terminates or the timeout elapses.- Specified by:
join
in interfaceJoinable<V>
- Specified by:
join
in classStrand
- Parameters:
timeout
- the maximum duration to wait for the strand to terminate in the time unit specified byunit
.unit
- the time unit oftimeout
.- Throws:
java.util.concurrent.ExecutionException
- if this strand has terminated as a result of an uncaught exception (which will be thecause
of the thrownExecutionException
.java.util.concurrent.TimeoutException
- if this strand did not terminate by the time the timeout has elapsed.java.lang.InterruptedException
-
joinNoSuspend
public final Fiber<V> joinNoSuspend() throws java.util.concurrent.ExecutionException, java.lang.InterruptedException
- Throws:
java.util.concurrent.ExecutionException
java.lang.InterruptedException
-
joinNoSuspend
public final Fiber<V> joinNoSuspend(long timeout, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.ExecutionException, java.lang.InterruptedException, java.util.concurrent.TimeoutException
- Throws:
java.util.concurrent.ExecutionException
java.lang.InterruptedException
java.util.concurrent.TimeoutException
-
get
@Suspendable public final V get() throws java.util.concurrent.ExecutionException, java.lang.InterruptedException
-
get
@Suspendable public final V get(long timeout, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.ExecutionException, java.lang.InterruptedException, java.util.concurrent.TimeoutException
-
isDone
public final boolean isDone()
-
cancel
public final boolean cancel(boolean mayInterruptIfRunning)
- Specified by:
cancel
in interfacejava.util.concurrent.Future<V>
-
isCancelled
public final boolean isCancelled()
- Specified by:
isCancelled
in interfacejava.util.concurrent.Future<V>
-
setUncaughtExceptionHandler
public final void setUncaughtExceptionHandler(Strand.UncaughtExceptionHandler eh)
Set the handler invoked when this fiber abruptly terminates due to an uncaught exception.A fiber can take full control of how it responds to uncaught exceptions by having its uncaught exception handler explicitly set.
- Specified by:
setUncaughtExceptionHandler
in classStrand
- Parameters:
eh
- the object to use as this fiber's uncaught exception handler. Ifnull
then this fiber has no explicit handler.- See Also:
setDefaultUncaughtExceptionHandler(co.paralleluniverse.strands.Strand.UncaughtExceptionHandler)
-
getUncaughtExceptionHandler
public final Strand.UncaughtExceptionHandler getUncaughtExceptionHandler()
Returns the handler invoked when this fiber abruptly terminates due to an uncaught exception.- Specified by:
getUncaughtExceptionHandler
in classStrand
-
getDefaultUncaughtExceptionHandler
public static Strand.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()
Returns the default handler invoked when a fiber abruptly terminates due to an uncaught exception. If the returned value isnull
, there is no default.
-
setDefaultUncaughtExceptionHandler
public static void setDefaultUncaughtExceptionHandler(Strand.UncaughtExceptionHandler eh)
Set the default handler invoked when a fiber abruptly terminates due to an uncaught exception, and no other handler has been defined for that fiber.- Parameters:
eh
- the object to use as the default uncaught exception handler. Ifnull
then there is no default handler.- See Also:
setUncaughtExceptionHandler(co.paralleluniverse.strands.Strand.UncaughtExceptionHandler)
,getUncaughtExceptionHandler()
-
getStackTrace
public final java.lang.StackTraceElement[] getStackTrace()
Description copied from class:Strand
Returns an array of stack trace elements representing the stack dump of this strand. This method will return a zero-length array if this strand has not started, has started but has not yet been scheduled to run by the system, or has terminated. If the returned array is of non-zero length then the first element of the array represents the top of the stack, which is the most recent method invocation in the sequence. The last element of the array represents the bottom of the stack, which is the least recent method invocation in the sequence.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.
- Specified by:
getStackTrace
in classStrand
- Returns:
- an array of
StackTraceElement
s, each represents one stack frame.
-
dumpStack
public static void dumpStack()
-
toString
public final java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
isRecordingLevel
protected final boolean isRecordingLevel(int level)
-
record
protected final void record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format)
-
record
protected final void record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object arg1)
-
record
protected final void record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object arg1, java.lang.Object arg2)
-
record
protected final void record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3)
-
record
protected final void record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4)
-
record
protected final void record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4, java.lang.Object arg5)
-
record
protected final void record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4, java.lang.Object arg5, java.lang.Object arg6)
-
record
protected final void record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4, java.lang.Object arg5, java.lang.Object arg6, java.lang.Object arg7)
-
record
protected final void record(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object... args)
-
parkAndSerialize
public static void parkAndSerialize(FiberWriter writer) throws SuspendExecution
Parks the fiber and allows the given callback to serialize it.- Parameters:
writer
- a callback that can serialize the fiber.- Throws:
SuspendExecution
-
parkAndCustomSerialize
public static void parkAndCustomSerialize(CustomFiberWriter writer) throws SuspendExecution
Parks the fiber and allows the given callback to serialize it, optimized for use cases where the callback object has a custom way to obtain the required serializer (e.g. from a serializer pool)- Parameters:
writer
- a callback that can serialize the fiber.- Throws:
SuspendExecution
-
unparkSerialized
public static <V> Fiber<V> unparkSerialized(byte[] serFiber, FiberScheduler scheduler)
Deserializes a fiber from the given byte array and unparks it.- Parameters:
serFiber
- The byte array containing a fiber's serialized form.scheduler
- TheFiberScheduler
to use for scheduling the fiber.- Returns:
- The deserialized, running fiber.
-
unparkDeserialized
public static <V> Fiber<V> unparkDeserialized(Fiber<V> f, FiberScheduler scheduler)
Unparks a fiber that's been deserialized (with the help ofgetFiberSerializer()
- Parameters:
f
- The deserialized fiberscheduler
- TheFiberScheduler
to use for scheduling the fiber.- Returns:
- The fiber
-
getFiberSerializer
public static co.paralleluniverse.io.serialization.ByteArraySerializer getFiberSerializer()
Returns aByteArraySerializer
capable of serializing an object graph containing fibers.
-
getFiberSerializer
public static co.paralleluniverse.io.serialization.ByteArraySerializer getFiberSerializer(boolean includeThreadLocals)
Returns aByteArraySerializer
capable of serializing an object graph containing fibers.- Parameters:
includeThreadLocals
- if true, thread/fiber local storage slots will also be serialised. You may want to set this to false if you are using frameworks that put things that cannot be properly serialised into TLS slots, or if the feature causes other issues.
-
-