shadow:standard

Class Thread

Visible inner classes

shadow:standard@Thread:Current

Parent class

shadow:standard@Object

Interfaces

shadow:standard@CanIndex<int,shadow:standard@Thread>

locked class Thread

Class Thread provides ways to manipulate threads. All threads are required to terminate for the main process to terminate as well. If the main method returns and there are still running threads ... what will happen?

Authors

Claude Abounegm

Barry Wittman

Create Summary

Modifiers Return Types Method and Description
protected () create(nullable String name, CanRun runner)

Creates a new Thread object which takes on a CanRun instance, containing a run function that is going to be run on a different thread, and takes a String which is the name of the thread for reference purposes.

protected () create(CanRun runner)

Creates a new Thread object which takes on a CanRun instance, containing a run function that is going to be ran on a different thread.

Destroy Summary

Modifiers Return Types Method and Description
public () destroy()

Method Summary

Modifiers Return Types Method and Description
protected locked () addChild(Thread thread)
public readonly locked (Thread) childAt(int index)

Gets the n-th child that this thread spawned.

public readonly locked (CanIterate<Thread>) children()

Gets the children of this thread.

public readonly locked (CanIterate<Thread>) children(boolean recurse)

Gets the children of this thread.

public readonly (Thread) copy(AddressMap addresses)
public readonly locked (Thread) index(int index)

Gets the n-th child that this thread spawned.

public locked () interrupt()

Requests this thread to be interrupted.

public locked () join()

Interruptible.

public locked (boolean) join(int millisecondsTimeout)

Interruptible.

public locked (boolean) join(TimeSpan timeout)

Interruptible.

protected locked (Object, Thread) receiveFirst(Class expectedType, boolean blocking)

Interruptible.

protected locked (Object) receiveFirstFrom(Class expectedType, Thread from, boolean blocking)

Interruptible.

protected locked () sendTo(Object data, boolean blocking)

Interruptible.

public readonly locked (String) toString()

Returns a string with the format: "Thread ({Thread->name}): {Thread->stateString}".

Property Summary

Modifiers Return Types Method and Description
public readonly get locked (int) childrenCount()

Gets the number of children the thread currently has.

public readonly get locked (int) id()

Gets the Shadow-specific thread ID.

public readonly get locked (boolean) isAlive()

Gets whether this thread is running.

public readonly get locked (String) name()

Gets the name of the thread set at spawn time.

public readonly get locked (nullable Thread) parent()

Gets the Thread which spawned this thread.

public readonly get locked (int) state()

Gets the state of this thread.

public readonly get locked (String) stateString()

Gets the string representation of the state of this thread.

Create Detail

create

protected create(nullable String name, CanRun runner) => ()

Creates a new Thread object which takes on a CanRun instance, containing a run function that is going to be run on a different thread, and takes a String which is the name of the thread for reference purposes.

Parameters

name - The name of the thread.

runner - The instance that contains a run function.

create

protected create(CanRun runner) => ()

Creates a new Thread object which takes on a CanRun instance, containing a run function that is going to be ran on a different thread.

Parameters

runner - The instance that contains a run function.

Destroy Detail

destroy

public destroy() => ()

Method Detail

addChild

protected locked addChild(Thread thread) => ()

childAt

public readonly locked childAt(int index) => (Thread)

Gets the n-th child that this thread spawned. This is NOT the Shadow-specific id.

Parameters

index - the index of the child in the range [0, childrenCount).

Returns

the n-th child Thread.

children

public readonly locked children() => (CanIterate<Thread>)

Gets the children of this thread. This method only enumerates through the first generation children of this thread. i.e. only the thread that this thread spawned.

Returns

CanIterate<Thread> An iterable class to be used with a foreach loop.

children

public readonly locked children(boolean recurse) => (CanIterate<Thread>)

Gets the children of this thread. If recurse is true, this method will also traverse the children of the children recursively, until all children have been visited. Care should be taken using this function, as enumerating can throw an exception if the children have changed.

Returns

CanIterate<Thread> An iterable class to be used with a foreach loop.

copy

public readonly copy(AddressMap addresses) => (Thread)

index

public readonly locked index(int index) => (Thread)

Gets the n-th child that this thread spawned. This is NOT the Shadow-specific id. This is a shortcut to Thread.childAt(), to allow easy indexing of children using Thread[index].

Parameters

index - the index of the child in the range [0, childrenCount).

Returns

the n-th child Thread.

interrupt

public locked interrupt() => ()

Requests this thread to be interrupted. If the thread is currently in the WAITING state, the thread is interrupted and the method that caused the thread to be in the WAITING state raises an InterruptedException. The methods which are interruptible are join(), sleep(), send(), receive(), and all their overloads. If the thread is not in the WAITING state, an interrupt flag is set, and the thread is interrupted as soon as one of the above interruptible methods above are called.

Returns

true if the thread was marked as interrupted or false if the thread wasn't waiting.

join

public locked join() => ()

Interruptible. Suspends the execution of the calling thread until this thread terminates. Joining on a dead thread has no side effects. If the thread is terminated because of an exception, the exception is thrown when this method is called.

Throws

InvalidOperationException - if this thread is the main thread or is the same as the calling thread.

ThreadException - if the thread terminated due to an exception.

InterruptedException - if another thread requested to interrupt this thread.

See Also

join

public locked join(int millisecondsTimeout) => (boolean)

Interruptible. Suspends the execution of the calling thread until this thread terminates or until it times out. Joining on a dead thread has no side effects. If the thread is terminated because of an exception, the exception is thrown when this method is called.

Parameters

millisecondsTimeout - the time to wait for in milliseconds.

Returns

true if the join returned because of a timeout; otherwise, false.

Throws

InvalidOperationException - if this thread is the main thread or is the same as the calling thread.

ThreadException - if the thread terminated due to an exception.

InterruptedException - if another thread requested to interrupt this thread.

See Also

join

public locked join(TimeSpan timeout) => (boolean)

Interruptible. Suspends the execution of the calling thread until this thread terminates or until it times out. Joining on a dead thread has no side effects. If the thread is terminated because of an exception, the exception is thrown when this method is called.

Parameters

timeout - the time to wait for.

Returns

true if the join returned because of a timeout; otherwise, false.

Throws

InvalidOperationException - if this thread is the main thread or is the same as the calling thread.

ThreadException - if the thread terminated due to an exception.

InterruptedException - if another thread requested to interrupt this thread.

See Also

receiveFirst

protected locked receiveFirst(Class expectedType, boolean blocking) => (Object, Thread)

Interruptible. Receives data with the desired expectedType. This method will throw an exception and will not retrieve the message if the expected type is not the same as the actual one sent by the thread. A thread can send as many messages as it desires, but the receiving thread should always receive the data in the order it was sent; otherwise, an exception is thrown.

Throws

IncompatibleMessageTypeException - If the item attempted to retrieve does not have the same type as expectedType.

InterruptedException - If this thread was interrupted while waiting on an empty mailbox.

IllegalArgumentException - If this thread is the same as the from thread.

receiveFirstFrom

protected locked receiveFirstFrom(Class expectedType, Thread from, boolean blocking) => (Object)

Interruptible. Receives data with the desired expectedType from the desired thread. This method will throw an exception and will not retrieve the message if the expected type is not the same as the actual one sent by the thread. A thread can send as many messages as it desires, but the receiving thread should always receive the data in the order it was sent, or otherwise an exception is thrown.

Throws

IncompatibleMessageTypeException - If the item attempted to retrieve does not have the same type as expectedType.

InterruptedException - If this thread was interrupted while waiting on an empty mailbox.

IllegalArgumentException - If this thread is the same as the from thread.

sendTo

protected locked sendTo(Object data, boolean blocking) => ()

Interruptible. Sends the desired data to this thread. This method copies all the data before sending it to this thread. Thus no data is shared between two threads. This is a blocking method, and it will block if the mailbox of this thread is full. The method will immediately unblock when there is space for the item to be deposited in the mailbox. Mailbox is first come first serve, and the threads sending data will unblock in the order they were blocked in.

Parameters

data - The data to be cloned and sent to this thread.

Throws

InterruptedException - If this thread was interrupted while waiting on a full mailbox.

InvalidOperationException - if this thread is dead or is the same as the receiving one.

toString

public readonly locked toString() => (String)

Returns a string with the format: "Thread ({Thread->name}): {Thread->stateString}".

Property Detail

childrenCount

public readonly get locked childrenCount() => (int)

Gets the number of children the thread currently has. This method should be used with caution even though it is thread safe. Since a thread can spawn threads at any time, this count can drastically change between each call.

Returns

how many children that thread has

id

public readonly get locked id() => (int)

Gets the Shadow-specific thread ID.

isAlive

public readonly get locked isAlive() => (boolean)

Gets whether this thread is running.

Returns

true if the thread is alive; otherwise, false.

name

public readonly get locked name() => (String)

Gets the name of the thread set at spawn time. If no name is set at spawn time, the name takes the format "Thread#{id}".

parent

public readonly get locked parent() => (nullable Thread)

Gets the Thread which spawned this thread. The Main thread does not have a parent. Once a thread has ended, its parent is set to null.

state

public readonly get locked state() => (int)

Gets the state of this thread. It can be one of READY, RUNNING, WAITING, or DEAD.

stateString

public readonly get locked stateString() => (String)

Gets the string representation of the state of this thread.