ThreadEx

A <i>thread</i> is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. <p> Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new {@code Thread} object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon. <p> When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named {@code main} of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs: <ul> <li>The {@code exit} method of class {@code Runtime} has been called and the security manager has permitted the exit operation to take place. <li>All threads that are not daemon threads have died, either by returning from the call to the {@code run} method or by throwing an exception that propagates beyond the {@code run} method. </ul> <p> There are two ways to create a new thread of execution. One is to declare a class to be a subclass of {@code Thread}. This subclass should override the {@code run} method of class {@code Thread}. An instance of the subclass can then be allocated and started. For example, a thread that computes primes larger than a stated value could be written as follows: <hr><blockquote><pre> class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; }

public void run() { // compute primes larger than minPrime &nbsp;.&nbsp;.&nbsp;. } } </pre></blockquote><hr> <p> The following code would then create a thread and start it running: <blockquote><pre> PrimeThread p = new PrimeThread(143); p.start(); </pre></blockquote> <p> The other way to create a thread is to declare a class that implements the {@code Runnable} interface. That class then implements the {@code run} method. An instance of the class can then be allocated, passed as an argument when creating {@code Thread}, and started. The same example in this other style looks like the following: <hr><blockquote><pre> class PrimeRun implements Runnable { long minPrime; PrimeRun(long minPrime) { this.minPrime = minPrime; }

public void run() { // compute primes larger than minPrime &nbsp;.&nbsp;.&nbsp;. } } </pre></blockquote><hr> <p> The following code would then create a thread and start it running: <blockquote><pre> PrimeRun p = new PrimeRun(143); new Thread(p).start(); </pre></blockquote> <p> Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it. <p> Unless otherwise noted, passing a {@code null} argument to a constructor or method in this class will cause a {@link NullPointerException} to be thrown.

Constructors

this
this()
Undocumented in source.
this
this(string name)
Undocumented in source.
this
this(Runnable target)
Undocumented in source.
this
this(Runnable target, string name)
Undocumented in source.
this
this(ThreadGroupEx group, string name)
Undocumented in source.
this
this(ThreadGroupEx group, Runnable target, string name, size_t sz)
Undocumented in source.
this
this(Action dg, string name)
Undocumented in source.
this
this(Action dg)
Undocumented in source.
this
this(void function() fn)
Undocumented in source.

Destructor

~this
~this()
Undocumented in source.

Members

Functions

blockedOn
void blockedOn(Interruptible b)
Undocumented in source. Be warned that the author may not have intended to support it.
checkAccess
void checkAccess()
Undocumented in source. Be warned that the author may not have intended to support it.
getState
ThreadState getState()

Returns the state of this thread. This method is designed for use in monitoring of the system state, not for synchronization control.

getThreadGroup
ThreadGroupEx getThreadGroup()

Returns the thread group to which this thread belongs. This method returns null if this thread has died (been stopped).

getUncaughtExceptionHandler
UncaughtExceptionHandler getUncaughtExceptionHandler()

Returns the handler invoked when this thread abruptly terminates due to an uncaught exception. If this thread has not had an uncaught exception handler explicitly set then this thread's {@code ThreadGroup} object is returned, unless this thread has terminated, in which case {@code null} is returned. @return the uncaught exception handler for this thread

interrupt
void interrupt()

Interrupts this thread.

isAlive
bool isAlive()

Tests if this thread is alive. A thread is alive if it has been started and has not yet died.

isInterrupted
bool isInterrupted()

Tests whether this thread has been interrupted. The <i>interrupted status</i> of the thread is unaffected by this method.

parker
Parker parker()
Undocumented in source. Be warned that the author may not have intended to support it.
run
void run()

If this thread was constructed using a separate {@code Runnable} run object, then that {@code Runnable} object's {@code run} method is called; otherwise, this method does nothing and returns. <p> Subclasses of {@code Thread} should override this method.

setDaemon
void setDaemon(bool on)

Marks this thread as either a {@linkplain #isDaemon daemon} thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.

setUncaughtExceptionHandler
void setUncaughtExceptionHandler(UncaughtExceptionHandler eh)

Set the handler invoked when this thread abruptly terminates due to an uncaught exception. <p>A thread can take full control of how it responds to uncaught exceptions by having its uncaught exception handler explicitly set. If no such handler is set then the thread's {@code ThreadGroup} object acts as its handler. @param eh the object to use as this thread's uncaught exception handler. If {@code null} then this thread has no explicit handler. @throws SecurityException if the current thread is not allowed to modify this thread. @see #setDefaultUncaughtExceptionHandler @see ThreadGroup#uncaughtException

Static functions

currentThread
ThreadEx currentThread()
Undocumented in source. Be warned that the author may not have intended to support it.
getDefaultUncaughtExceptionHandler
UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()

Returns the default handler invoked when a thread abruptly terminates due to an uncaught exception. If the returned value is {@code null}, there is no default. @see #setDefaultUncaughtExceptionHandler @return the default uncaught exception handler for all threads

infiniteSleep
void infiniteSleep()
Undocumented in source. Be warned that the author may not have intended to support it.
interrupted
bool interrupted()

Tests whether the current thread has been interrupted. The <i>interrupted status</i> of the thread is cleared by this method. In other words, if this method were to be called twice in succession, the second call would return false (unless the current thread were interrupted again, after the first call had cleared its interrupted status and before the second call had examined it).

nakedSleep
void nakedSleep(Duration timeout)
Undocumented in source. Be warned that the author may not have intended to support it.
setDefaultUncaughtExceptionHandler
void setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh)

Set the default handler invoked when a thread abruptly terminates due to an uncaught exception, and no other handler has been defined for that thread.

sleep
void sleep(Duration timeout)
Undocumented in source. Be warned that the author may not have intended to support it.

Variables

state
ThreadState state;
Undocumented in source.

Meta