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 toVoidif 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
Fiberclass and override therunmethod, or pass the code to be executed in the fiber as thetargetparameter to the constructor. All in all, the Fiber API resembles theThreadclass 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 intDEFAULT_STACK_SIZEprotected static co.paralleluniverse.common.monitoring.FlightRecorderflightRecorder-
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 therunmethod.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 therunmethod.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 therunmethod.Fiber(java.lang.String name, int stackSize)Creates a new child Fiber subclassing the Fiber class and overriding therunmethod.Fiber(java.lang.String name, int stackSize, SuspendableCallable<V> target)Creates a new fiber from the givenSuspendableCallablescheduled in thedefault fiber schedulerFiber(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 therunmethod.Fiber(java.lang.String name, FiberScheduler scheduler, int stackSize)Creates a new Fiber subclassing the Fiber class and overriding therunmethod.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 booleancancel(boolean mayInterruptIfRunning)static FibercurrentFiber()Returns the active Fiber on this thread or NULL if no Fiber is running.static voiddumpStack()booleanequals(java.lang.Object obj)Vget()Vget(long timeout, java.util.concurrent.TimeUnit unit)java.lang.ObjectgetBlocker()Returns the blocker object supplied to the most recent invocation of aparkmethod that has not yet unblocked, or null if not blocked.static longgetCurrentRun()static Strand.UncaughtExceptionHandlergetDefaultUncaughtExceptionHandler()Returns the default handler invoked when a fiber abruptly terminates due to an uncaught exception.static co.paralleluniverse.io.serialization.ByteArraySerializergetFiberSerializer()Returns aByteArraySerializercapable of serializing an object graph containing fibers.static co.paralleluniverse.io.serialization.ByteArraySerializergetFiberSerializer(boolean includeThreadLocals)Returns aByteArraySerializercapable of serializing an object graph containing fibers.longgetId()Returns the strand's id.java.lang.InterruptedExceptiongetInterruptStack()Returns anInterruptedExceptionthat 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.FibersMonitorgetMonitor()java.lang.StringgetName()Returns the strand's name.booleangetNoLocals()Deprecated.intgetPriority()Returns this fiber's priority.FiberSchedulergetScheduler()java.lang.StackTraceElement[]getStackTrace()Returns an array of stack trace elements representing the stack dump of this strand.Strand.StategetState()Returns the strand's current running state.SuspendableCallable<V>getTarget()java.lang.ObjectgetTask()Strand.UncaughtExceptionHandlergetUncaughtExceptionHandler()Returns the handler invoked when this fiber abruptly terminates due to an uncaught exception.java.lang.ObjectgetUnderlying()Returns the underlying object of this strand, namely aThreador aFiber.inthashCode()FiberinheritThreadLocals()Causes the current strand'sthread-localsto be inherited by this fiber.voidinterrupt()Interrupts this strand.static booleaninterrupted()booleanisAlive()Tests whether this strand is alive, namely it has been started but not yet terminated.booleanisCancelled()static booleanisCurrentFiber()Tests whether current code is executing in a fiber.booleanisDone()booleanisFiber()Tests whether this strand is a fiber.booleanisInterrupted()Tests whether this strand has been interrupted.protected booleanisRecordingLevel(int level)booleanisTerminated()Tests whether this strand has terminated.voidjoin()Awaits the termination of this strand.voidjoin(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 voidonCompletion()protected voidonException(java.lang.Throwable t)protected voidonParked()protected voidonResume()static voidpark()static voidpark(long timeout, java.util.concurrent.TimeUnit unit)static voidpark(java.lang.Object blocker)static booleanpark(java.lang.Object blocker, long timeout, java.util.concurrent.TimeUnit unit)static voidparkAndCustomSerialize(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 voidparkAndSerialize(FiberWriter writer)Parks the fiber and allows the given callback to serialize it.static voidparkAndUnpark(Fiber other)static voidparkAndUnpark(Fiber other, java.lang.Object blocker)protected voidrecord(int level, java.lang.String clazz, java.lang.String method, java.lang.String format)protected voidrecord(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object arg1)protected voidrecord(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object... args)protected voidrecord(int level, java.lang.String clazz, java.lang.String method, java.lang.String format, java.lang.Object arg1, java.lang.Object arg2)protected voidrecord(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 voidrecord(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 voidrecord(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 voidrecord(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 voidrecord(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 Vrun()static voidsetDefaultUncaughtExceptionHandler(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.FibersetNoLocals(boolean value)Deprecated.FibersetPriority(int newPriority)Sets the priority of this fiber.voidsetUncaughtExceptionHandler(Strand.UncaughtExceptionHandler eh)Set the handler invoked when this fiber abruptly terminates due to an uncaught exception.protected booleanshouldPreempt(int type)static voidsleep(long millis)static voidsleep(long millis, int nanos)static voidsleep(long duration, java.util.concurrent.TimeUnit unit)Fiber<V>start()Starts the strand.java.lang.StringtoString()voidunpark()Makes available the permit for this fiber, if it was not already available.voidunpark(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 voidyield()static voidyieldAndUnpark(Fiber other)static voidyieldAndUnpark(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- TheFiberSchedulerthat will schedule the fiber.stackSize- the initial size of the data stack.target- theSuspendableCallablefor the fiber. Maynullif therunmethod is overriden.
-
Fiber
public Fiber(java.lang.String name, int stackSize, SuspendableCallable<V> target)Creates a new fiber from the givenSuspendableCallablescheduled in thedefault fiber scheduler- Parameters:
name- The name of the fiber (may benull)stackSize- the initial size of the data stack.target- theSuspendableCallablefor the fiber. Maynullif therunmethod 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 therunmethod.- 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 therunmethod. 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 therunmethod. 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 therunmethod. 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 therunmethod. 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 therunmethod. 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:
hashCodein classjava.lang.Object
-
equals
public final boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
getName
public final java.lang.String getName()
Description copied from class:StrandReturns the strand's name.
-
setName
public final Fiber<V> setName(java.lang.String name)
Description copied from class:StrandSets 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:
setPriorityin classStrand- Parameters:
newPriority- priority to set this fiber to- Throws:
java.lang.IllegalArgumentException- If the priority is not in the rangeMIN_PRIORITYtoMAX_PRIORITY- See Also:
getPriority(),Strand.MAX_PRIORITY,Strand.MIN_PRIORITY
-
getPriority
public int getPriority()
Returns this fiber's priority.- Specified by:
getPriorityin classStrand- Returns:
- this strand's priority.
- See Also:
setPriority(int)
-
getId
public long getId()
Description copied from class:StrandReturns 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:
trueif called in a fiber;falseotherwise.
-
getCurrentRun
public static long getCurrentRun()
-
isFiber
public final boolean isFiber()
Description copied from class:StrandTests whether this strand is a fiber.
-
getUnderlying
public final java.lang.Object getUnderlying()
Description copied from class:StrandReturns the underlying object of this strand, namely aThreador aFiber.- Specified by:
getUnderlyingin 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.InterruptedExceptionSuspendExecution
-
sleep
public static void sleep(long millis, int nanos) throws java.lang.InterruptedException, SuspendExecution- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
sleep
public static void sleep(long duration, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, SuspendExecution- Throws:
java.lang.InterruptedExceptionSuspendExecution
-
interrupted
public static boolean interrupted()
-
getMonitor
public co.paralleluniverse.fibers.FibersMonitor getMonitor()
-
run
protected V run() throws SuspendExecution, java.lang.InterruptedException
- Throws:
SuspendExecutionjava.lang.InterruptedException
-
inheritThreadLocals
public Fiber inheritThreadLocals()
Causes the current strand'sthread-localsto be inherited by this fiber. By default onlyInheritableThreadLocals are inherited.This method must be called before the fiber is started (i.e. before the
startmethod is called. Otherwise, anIllegalStateExceptionis thrown.- Returns:
this
-
onParked
protected void onParked()
-
onResume
protected void onResume() throws SuspendExecution, java.lang.InterruptedException- Throws:
SuspendExecutionjava.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:StrandInterrupts 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:StrandTests whether this strand has been interrupted.- Specified by:
isInterruptedin classStrand- Returns:
trueif the strand has been interrupted;falseotherwise.- See Also:
Strand.interrupt(),Strand.interrupted()
-
getInterruptStack
public final java.lang.InterruptedException getInterruptStack()
Description copied from class:StrandReturns anInterruptedExceptionthat 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 returnnullif this information is not available. The current implementation always returnsnullif this strand is a thread.- Specified by:
getInterruptStackin classStrand
-
isAlive
public final boolean isAlive()
Description copied from class:StrandTests whether this strand is alive, namely it has been started but not yet terminated.
-
getState
public final Strand.State getState()
Description copied from class:StrandReturns the strand's current running state.
-
isTerminated
public final boolean isTerminated()
Description copied from class:StrandTests whether this strand has terminated.- Specified by:
isTerminatedin classStrand
-
getBlocker
public final java.lang.Object getBlocker()
Description copied from class:StrandReturns the blocker object supplied to the most recent invocation of aparkmethod 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:
getBlockerin 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 onparkthen it will unblock. Otherwise, its next call toparkis guaranteed not to block.
-
unpark
public final void unpark(java.lang.Object unblocker)
Description copied from class:StrandMakes 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:StrandAwaits 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:StrandAwaits 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:
joinin interfaceJoinable<V>- Specified by:
joinin 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 thecauseof 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.ExecutionExceptionjava.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.ExecutionExceptionjava.lang.InterruptedExceptionjava.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:
cancelin interfacejava.util.concurrent.Future<V>
-
isCancelled
public final boolean isCancelled()
- Specified by:
isCancelledin 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:
setUncaughtExceptionHandlerin classStrand- Parameters:
eh- the object to use as this fiber's uncaught exception handler. Ifnullthen 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:
getUncaughtExceptionHandlerin 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. Ifnullthen 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:StrandReturns 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:
getStackTracein classStrand- Returns:
- an array of
StackTraceElements, each represents one stack frame.
-
dumpStack
public static void dumpStack()
-
toString
public final java.lang.String toString()
- Overrides:
toStringin 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- TheFiberSchedulerto 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- TheFiberSchedulerto use for scheduling the fiber.- Returns:
- The fiber
-
getFiberSerializer
public static co.paralleluniverse.io.serialization.ByteArraySerializer getFiberSerializer()
Returns aByteArraySerializercapable of serializing an object graph containing fibers.
-
getFiberSerializer
public static co.paralleluniverse.io.serialization.ByteArraySerializer getFiberSerializer(boolean includeThreadLocals)
Returns aByteArraySerializercapable 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.
-
-