activemq-cpp-3.6.0
decaf::lang::Thread Class Reference

A Thread is a concurrent unit of execution. More...

#include <src/main/decaf/lang/Thread.h>

Inheritance diagram for decaf::lang::Thread:

Data Structures

class  UncaughtExceptionHandler
 Interface for handlers invoked when a Thread abruptly terminates due to an uncaught exception. More...

Public Types

enum  State {
  NEW = 0, RUNNABLE = 1, BLOCKED = 2, WAITING = 3,
  TIMED_WAITING = 4, SLEEPING = 5, TERMINATED = 6
}
 Represents the various states that the Thread can be in during its lifetime. More...

Public Member Functions

 Thread ()
 Constructs a new Thread.
 Thread (Runnable *task)
 Constructs a new Thread with the given target Runnable task.
 Thread (const std::string &name)
 Constructs a new Thread with the given name.
 Thread (Runnable *task, const std::string &name)
 Constructs a new Thread with the given target Runnable task and name.
 Thread (Runnable *task, const std::string &name, long long stackSize)
 Constructs a new Thread with the given target Runnable task and name.
virtual ~Thread ()
virtual void start ()
 Creates a system thread and starts it in a joinable mode.
virtual void join ()
 Forces the Current Thread to wait until the thread exits.
virtual void join (long long millisecs)
 Forces the Current Thread to wait until the thread exits.
virtual void join (long long millisecs, int nanos)
 Forces the Current Thread to wait until the thread exits.
virtual void run ()
 Default implementation of the run method - does nothing.
long long getId () const
 Obtains the Thread Id of the current thread, this value is OS specific but is guaranteed not to change for the lifetime of this thread.
std::string getName () const
 Returns the Thread's assigned name.
void setName (const std::string &name)
 Sets the name of the Thread to the new Name given by the argument name
int getPriority () const
 Gets the currently set priority for this Thread.
void setPriority (int value)
 Sets the current Thread's priority to the newly specified value.
UncaughtExceptionHandlergetUncaughtExceptionHandler () const
 Set the handler invoked when this thread abruptly terminates due to an uncaught exception.
void setUncaughtExceptionHandler (UncaughtExceptionHandler *handler)
 Set the handler invoked when this thread abruptly terminates due to an uncaught exception.
std::string toString () const
 Returns a string that describes the Thread.
bool isAlive () const
 Returns true if the Thread is alive, meaning it has been started and has not yet died.
Thread::State getState () const
 Returns the currently set State of this Thread.
void interrupt ()
 Interrupts the Thread if it is blocked and in an interruptible state.
bool isInterrupted () const
 Returns but does not clear the state of this Thread's interrupted flag.
- Public Member Functions inherited from decaf::lang::Runnable
virtual ~Runnable ()

Static Public Member Functions

static void sleep (long long millisecs)
 Causes the currently executing thread to halt execution for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.
static void sleep (long long millisecs, int nanos)
 Causes the currently executing thread to halt execution for the specified number of milliseconds plus any additionally specified nanoseconds given, subject to the precision and accuracy of system timers and schedulers.
static void yield ()
 Causes the currently executing thread object to temporarily pause and allow other threads to execute.
static ThreadcurrentThread ()
 Returns a pointer to the currently executing thread object.
static bool interrupted ()
 Returns whether the thread has been interrupted and clears the interrupted state such that a subsequent call will return false unless an interrupt occurs between the two calls.
static UncaughtExceptionHandlergetDefaultUncaughtExceptionHandler ()
 Set the default handler invoked when a thread abruptly terminates due to an uncaught exception, this handler is used only if there is no other handler defined for the Thread.
static void setDefaultUncaughtExceptionHandler (UncaughtExceptionHandler *handler)
 Set the default handler invoked when a thread abruptly terminates due to an uncaught exception,.

Static Public Attributes

static const int MIN_PRIORITY = 1
 The minimum priority that a thread can have.
static const int NORM_PRIORITY = 5
 The default priority that a thread is given at create time.
static const int MAX_PRIORITY = 10
 The maximum priority that a thread can have.

Friends

class decaf::internal::util::concurrent::Threading
class ThreadGroup

Detailed Description

A Thread is a concurrent unit of execution.

It has its own call stack for methods being invoked, their arguments and local variables. Each process has at least one main Thread running when it is started; typically, there are several others for housekeeping. The application might decide to launch additional Threads for specific purposes.

Threads in the same process interact and synchronize by the use of shared objects and monitors associated with these objects.

There are basically two main ways of having a Thread execute application code. One is providing a new class that extends Thread and overriding its run() method. The other is providing a new Thread instance with a Runnable object during its creation. In both cases, the start() method must be called to actually execute the new Thread.

Each Thread has an integer priority that basically determines the amount of CPU time the Thread gets. It can be set using the setPriority(int) method.

See Also
decaf.lang.ThreadGroup
Since
1.0

Member Enumeration Documentation

Represents the various states that the Thread can be in during its lifetime.

Enumerator:
NEW 

Before a Thread is started it exists in this State.

RUNNABLE 

While a Thread is running and is not blocked it is in this State.

BLOCKED 

A Thread that is waiting to acquire a lock is in this state.

WAITING 

A Thread that is waiting for another Thread to perform an action is in this state.

TIMED_WAITING 

A Thread that is waiting for another Thread to perform an action up to a specified time interval is in this state.

SLEEPING 

A Thread that is blocked in a Sleep call is in this state.

TERMINATED 

A Thread whose run method has exited is in this state.

Constructor & Destructor Documentation

decaf::lang::Thread::Thread ( )

Constructs a new Thread.

This constructor has the same effect as Thread( NULL, NULL, GIVEN_NAME ), where GIVEN_NAME is a newly generated name. When no name is given the name is automatically generated and are of the form "Thread-"+n, where n is an integer.

decaf::lang::Thread::Thread ( Runnable task)

Constructs a new Thread with the given target Runnable task.

This constructor has the same effect as Thread( NULL, task, GIVEN_NAME ), where GIVEN_NAME is a newly generated name. When no name is given the name is automatically generated and are of the form "Thread-"+n, where n is an integer.

Parameters
taskthe Runnable that this thread manages, if the task is NULL the Thread's run method is used instead.
decaf::lang::Thread::Thread ( const std::string &  name)

Constructs a new Thread with the given name.

This constructor has the same effect as Thread( NULL, NULL, GIVEN_NAME ), where GIVEN_NAME is a newly generated name. When no name is given the name is automatically generated and are of the form "Thread-"+n, where n is an integer.

Parameters
namethe name to assign to this Thread.
decaf::lang::Thread::Thread ( Runnable task,
const std::string &  name 
)

Constructs a new Thread with the given target Runnable task and name.

This constructor has the same effect as Thread( NULL, task, GIVEN_NAME ), where GIVEN_NAME is a newly generated name. When no name is given the name is automatically generated and are of the form "Thread-"+n, where n is an integer.

Parameters
taskthe Runnable that this thread manages, if the task is NULL the Thread's run method is used instead.
namethe name to assign to this Thread.
decaf::lang::Thread::Thread ( Runnable task,
const std::string &  name,
long long  stackSize 
)

Constructs a new Thread with the given target Runnable task and name.

This constructor has the same effect as Thread( NULL, task, GIVEN_NAME ), where GIVEN_NAME is a newly generated name. When no name is given the name is automatically generated and are of the form "Thread-"+n, where n is an integer.

The stack size option is platform independent and may have no effect on the newly created thread on some systems. If the value given is invalid on the system a RuntimeException is thrown, the stack size can be invalid if it is outside the allowed range or doesn't match the size of the system page size on some system.

Parameters
taskThe Runnable that this thread manages, if the task is NULL the Thread's run method is used instead.
nameThe name to assign to this Thread.
stackSizeThe size of the newly allocated thread's stack.
virtual decaf::lang::Thread::~Thread ( )
virtual

Member Function Documentation

static Thread* decaf::lang::Thread::currentThread ( )
static

Returns a pointer to the currently executing thread object.

Returns
Pointer to the Thread object representing the currently running Thread.
static UncaughtExceptionHandler* decaf::lang::Thread::getDefaultUncaughtExceptionHandler ( )
static

Set the default handler invoked when a thread abruptly terminates due to an uncaught exception, this handler is used only if there is no other handler defined for the Thread.

This method will return NULL if no handler has ever been set, or the handler is cleared via a call to the setDefaultUncaughtExceptionHandler method will NULL as the value of the handler argument.

Returns
a pointer to the default UncaughtExceptionHandler for all Threads.
long long decaf::lang::Thread::getId ( ) const

Obtains the Thread Id of the current thread, this value is OS specific but is guaranteed not to change for the lifetime of this thread.

Returns
Thread Id of this Thread instance.
std::string decaf::lang::Thread::getName ( ) const

Returns the Thread's assigned name.

Returns
the Name of the Thread.
int decaf::lang::Thread::getPriority ( ) const

Gets the currently set priority for this Thread.

Returns
an int value representing the Thread's current priority.
Thread::State decaf::lang::Thread::getState ( ) const

Returns the currently set State of this Thread.

Returns
the Thread's current state.
UncaughtExceptionHandler* decaf::lang::Thread::getUncaughtExceptionHandler ( ) const

Set the handler invoked when this thread abruptly terminates due to an uncaught exception.

Returns
a pointer to the set UncaughtExceptionHandler.
void decaf::lang::Thread::interrupt ( )

Interrupts the Thread if it is blocked and in an interruptible state.

When the thread is in one of its own join or sleep methods or blocked by a call to a monitor or mutex wait call it will clear its interrupted flag and and an InterruptedException will be thrown.

In other cases the thread's interrupted status will be set and an instance of an InterruptedException may be thrown.

If the thread is not alive when this method is called there is no affect.

static bool decaf::lang::Thread::interrupted ( )
static

Returns whether the thread has been interrupted and clears the interrupted state such that a subsequent call will return false unless an interrupt occurs between the two calls.

Returns
true if the thread was interrupted, false otherwise.
bool decaf::lang::Thread::isAlive ( ) const

Returns true if the Thread is alive, meaning it has been started and has not yet died.

Returns
true if the thread is alive.
bool decaf::lang::Thread::isInterrupted ( ) const

Returns but does not clear the state of this Thread's interrupted flag.

Returns
true if the thread was interrupted, false otherwise.
virtual void decaf::lang::Thread::join ( )
virtual

Forces the Current Thread to wait until the thread exits.

Exceptions
InterruptedExceptionif any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
virtual void decaf::lang::Thread::join ( long long  millisecs)
virtual

Forces the Current Thread to wait until the thread exits.

Parameters
millisecsthe time in Milliseconds before the thread resumes
Exceptions
IllegalArgumentExceptionif the milliseconds parameter is negative.
InterruptedExceptionif any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
virtual void decaf::lang::Thread::join ( long long  millisecs,
int  nanos 
)
virtual

Forces the Current Thread to wait until the thread exits.

Parameters
millisecsthe time in Milliseconds before the thread resumes
nanos0-999999 extra nanoseconds to sleep.
Exceptions
IllegalArgumentExceptionif the nanoseconds parameter is out of range or the milliseconds paramter is negative.
InterruptedExceptionif any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
virtual void decaf::lang::Thread::run ( )
virtual

Default implementation of the run method - does nothing.

Implements decaf::lang::Runnable.

Reimplemented in activemq::transport::mock::InternalCommandListener.

static void decaf::lang::Thread::setDefaultUncaughtExceptionHandler ( UncaughtExceptionHandler handler)
static

Set the default handler invoked when a thread abruptly terminates due to an uncaught exception,.

Parameters
handlerThe UncaightExceptionHandler to invoke when a Thread terminates due to an uncaught exception, passing NULL clears this value.
void decaf::lang::Thread::setName ( const std::string &  name)

Sets the name of the Thread to the new Name given by the argument name

name the new name of the Thread.

void decaf::lang::Thread::setPriority ( int  value)

Sets the current Thread's priority to the newly specified value.

The given value must be within the range Thread::MIN_PRIORITY and Thread::MAX_PRIORITY.

Parameters
valuethe new priority value to assign to this Thread.
Exceptions
IllegalArgumentExceptionif the value is out of range.
void decaf::lang::Thread::setUncaughtExceptionHandler ( UncaughtExceptionHandler handler)

Set the handler invoked when this thread abruptly terminates due to an uncaught exception.

Parameters
handlerthe UncaightExceptionHandler to invoke when the Thread terminates due to an uncaught exception.
static void decaf::lang::Thread::sleep ( long long  millisecs)
static

Causes the currently executing thread to halt execution for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.

Note that this method is a static method that applies to the calling thread and not to the thread object.

Parameters
millisecstime in milliseconds to halt execution.
Exceptions
IllegalArgumentExceptionif the milliseconds parameter is negative.
InterruptedExceptionif the Thread was interrupted while sleeping.
static void decaf::lang::Thread::sleep ( long long  millisecs,
int  nanos 
)
static

Causes the currently executing thread to halt execution for the specified number of milliseconds plus any additionally specified nanoseconds given, subject to the precision and accuracy of system timers and schedulers.

Note that this method is a static method that applies to the calling thread and not to the thread object.

Parameters
millisecstime in milliseconds to halt execution.
nanos0-999999 extra nanoseconds to sleep.
Exceptions
IllegalArgumentExceptionif the nanoseconds parameter is out of range or the milliseconds paramter is negative.
InterruptedExceptionif the Thread was interrupted while sleeping.
virtual void decaf::lang::Thread::start ( )
virtual

Creates a system thread and starts it in a joinable mode.

Upon creation, the run() method of either this object or the provided Runnable object will be invoked in the context of this thread.

Exceptions
IllegalThreadStateExceptionif the thread has already been started.
RuntimeExceptionif the Thread cannot be created for some reason.
std::string decaf::lang::Thread::toString ( ) const

Returns a string that describes the Thread.

Returns
string describing the Thread.
static void decaf::lang::Thread::yield ( )
static

Causes the currently executing thread object to temporarily pause and allow other threads to execute.

Friends And Related Function Documentation

friend class ThreadGroup
friend

Field Documentation

const int decaf::lang::Thread::MAX_PRIORITY = 10
static

The maximum priority that a thread can have.

const int decaf::lang::Thread::MIN_PRIORITY = 1
static

The minimum priority that a thread can have.

const int decaf::lang::Thread::NORM_PRIORITY = 5
static

The default priority that a thread is given at create time.


The documentation for this class was generated from the following file: