#include <coherence/lang/Thread.hpp>
Inherits Object, and Runnable.
Public Types | |
typedef spec::Handle | Handle |
Thread Handle definition. | |
typedef spec::View | View |
Thread View definition. | |
typedef spec::Holder | Holder |
Thread Holder definition. | |
Public Member Functions | |
virtual void | run () |
Invoke the Runnable. | |
virtual int64_t | getId () const |
Return the Thread's unique ID. | |
virtual State | getState () const |
Return the Thread's current state. | |
virtual String::View | getName () const |
Return the Thread's name. | |
virtual ThreadGroup::Handle | getThreadGroup () |
Return this thread's group, or NULL if the thread has terminated. | |
virtual ThreadGroup::View | getThreadGroup () const |
Return this thread's group, or NULL if the thread has terminated. | |
virtual bool | isAlive () const |
Tests if this thread is alive. | |
virtual void | start () |
Begin executing the Thread's runnable on a new system thread. | |
virtual void | interrupt () |
Request that the Thread stop executing. | |
virtual bool | isInterrupted () const |
Return true iff the thread is interrupted. | |
virtual void | join () const |
Wait for the Thread to reach the state_terminated state. | |
virtual void | join (int64_t cMillis) const |
Wait for the Thread to reach the state_terminated state. | |
virtual void | setName (String::View vsName) |
Set the Thread's name. | |
virtual TypedHandle < const String > | toString () const |
Output a human-readable description of this Object to the given stream. Note that when overriding this method the return type must be TypedHandle<const String> rather then String::View. These two types are assignment compatible but not equivalent and declaring the override with String::View will not be a compatible override. coherence::lang::operator<<(std::ostream, Object::View) is defined and will call into the toString method, to output Objects. If a managed String object is desired, the COH_TO_STRING macro can be used to build up a String from streamable contents and is generally how toString() will be implemented.
Object::View vKey = ... Object::View vValue = ... std::cout << vKey << " = " << vValue << std::endl; String::View vs = COH_TO_STRING(vKey << " = " << vValue); The COH_TO_STRING macro is also the most common way to implement the toString method. For example:
virtual TypedHandle<const String> Person::toString() const { return COH_TO_STRING("Name: " << f_sName << " SSN: " << f_nSSN); }
| |
Static Public Member Functions | |
static Thread::Handle | currentThread () |
Return the Thread on which the caller is executing. | |
static ObjectArray::Handle | getStackTrace (size32_t cTrim=0) |
Return a stack trace for this thread. | |
static String::View | formatStackTrace (ObjectArray::View vaStack) |
Format a stack trace. | |
static String::View | formatStacks (int64_t cMillisTimeout=-1) |
Return the stack trace of all known threads as a String. | |
static bool | interrupted () |
Return true iff the caller's thread has been interrupted, or timed out. | |
static void | sleep (int64_t cMillis) |
Pause the current thread for the specified duration. | |
static void | yield () |
Temporarily pause the current thread. | |
static int64_t | remainingTimeoutMillis () |
Return the number of milliseconds before the calling thread will timeout. | |
static bool | isTimedOut () |
Return true if the calling thread is timed out. | |
Protected Types | |
enum | Maintenance |
Defines various thread maintenance tasks. | |
Protected Member Functions | |
virtual void | onExit () |
Called automatically on the thread prior to termination. | |
virtual void | scheduleMaintenance (Maintenance nMaintenance) const |
Instruct the thread to perform maintenance. | |
Static Protected Member Functions | |
static void | doGlobalMaintenance (Maintenance nMaintenance) |
Instruct all threads to perform maintenance. | |
Protected Attributes | |
MemberHandle < ThreadGroup > | m_hGroup |
The thread's group. | |
MemberHandle< Runnable > | m_hRunnable |
The inner runnable associated with this Thread. | |
MemberView< String > | m_vsName |
The thread's name. | |
FinalView< String > | f_vsDescription |
The thread's description. | |
size32_t | m_cBytesStack |
The configured stack size;. | |
Volatile< int32_t > | m_nState |
The current thread state. | |
int | m_nMaintenanceMask |
Bit mask identifying required maintenance tasks. | |
int64_t | m_lTimeout |
The thread's timeout value. |
virtual int64_t getId | ( | ) | const [virtual] |
Return the Thread's unique ID.
No two live thread's will have the same ID and no thread will have an id of 0.
virtual State getState | ( | ) | const [virtual] |
Return the Thread's current state.
virtual String::View getName | ( | ) | const [virtual] |
Return the Thread's name.
virtual ThreadGroup::Handle getThreadGroup | ( | ) | [virtual] |
Return this thread's group, or NULL if the thread has terminated.
virtual ThreadGroup::View getThreadGroup | ( | ) | const [virtual] |
Return this thread's group, or NULL if the thread has terminated.
virtual bool isAlive | ( | ) | const [virtual] |
Tests if this thread is alive.
A thread is alive if it has been started and has not yet died.
true
if this thread is alive; false
otherwise. virtual void start | ( | ) | [virtual] |
Begin executing the Thread's runnable on a new system thread.
This method may only be called once per Thread instance.
IllegalStateException | if a called on a Thread which is not in the state state_initial. |
virtual bool isInterrupted | ( | ) | const [virtual] |
Return true iff the thread is interrupted.
Note, Unlike Thread::interrupted() this does not check if the thread has timed-out.
virtual void join | ( | int64_t | cMillis | ) | const [virtual] |
Wait for the Thread to reach the state_terminated state.
cMillis | the maximum duration to wait, or zero for infinite |
virtual void setName | ( | String::View | vsName | ) | [virtual] |
Set the Thread's name.
vsName | the Thread's name |
virtual void onExit | ( | ) | [protected, virtual] |
static Thread::Handle currentThread | ( | ) | [static] |
static ObjectArray::Handle getStackTrace | ( | size32_t | cTrim = 0 |
) | [static] |
Return a stack trace for this thread.
cTrim | the number of frames to trim from the top of the stack |
static String::View formatStackTrace | ( | ObjectArray::View | vaStack | ) | [static] |
Format a stack trace.
vaStack | the array of StackTraceElements to output, or NULL for the stack of the current thread |
static String::View formatStacks | ( | int64_t | cMillisTimeout = -1 |
) | [static] |
Return the stack trace of all known threads as a String.
This is a blocking operation, while the calling thread waits for all known threads to report their stack.
cMillisTimeout | the maximum amount of time to wait for all threads to report their stacks, or -1 for the system default of twice the interrupt resolution |
static bool interrupted | ( | ) | [static] |
Return true iff the caller's thread has been interrupted, or timed out.
The thread's interrupt state is reset as part of this operation.
static void sleep | ( | int64_t | cMillis | ) | [static] |
Pause the current thread for the specified duration.
cMillis | the duration to pause the thread for. |
InterruptedException | if the thread is interrupted while sleeping. |
static int64_t remainingTimeoutMillis | ( | ) | [static] |
Return the number of milliseconds before the calling thread will timeout.
Note if the current thread is timed out then invoking this method will also interrupt the thread.
static bool isTimedOut | ( | ) | [static] |
Return true if the calling thread is timed out.
Note if the current thread is timed out then invoking this method will also interrupt the thread.
virtual TypedHandle<const String> toString | ( | ) | const [virtual] |
Output a human-readable description of this Object to the given stream.
Note that when overriding this method the return type must be TypedHandle<const String> rather then String::View. These two types are assignment compatible but not equivalent and declaring the override with String::View will not be a compatible override.
coherence::lang::operator<<(std::ostream, Object::View) is defined and will call into the toString method, to output Objects. If a managed String object is desired, the COH_TO_STRING macro can be used to build up a String from streamable contents and is generally how toString() will be implemented.
Object::View vKey = ... Object::View vValue = ... std::cout << vKey << " = " << vValue << std::endl; String::View vs = COH_TO_STRING(vKey << " = " << vValue);
The COH_TO_STRING macro is also the most common way to implement the toString method. For example:
virtual TypedHandle<const String> Person::toString() const { return COH_TO_STRING("Name: " << f_sName << " SSN: " << f_nSSN); }
If the associated Runnable implements the coherence::util::Describable interface that content will be included as well.
Reimplemented from Object.
int64_t m_lTimeout [protected] |
The thread's timeout value.
Value which are greater or equal to zero are used to indicate timeout timestamps. Negative values are relative timeouts which haven't yet been realized into a timestamp. This allows for an optimization where we can avoid obtaining the current time when "setting" the timeout, and defer it until we are about to block.