DthingApi
Classes | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
java.lang.Thread Class Reference
Inheritance diagram for java.lang.Thread:
java.lang.Runnable

Classes

interface  UncaughtExceptionHandler
 

Public Member Functions

 Thread ()
 
 Thread (Runnable runnable)
 
 Thread (Runnable runnable, String threadName)
 
 Thread (String threadName)
 
long getId ()
 
final String getName ()
 
final int getPriority ()
 
UncaughtExceptionHandler getUncaughtExceptionHandler ()
 
native void interrupt ()
 
final native boolean isAlive ()
 
native boolean isInterrupted ()
 
final void join () throws InterruptedException
 
final void join (long millis) throws InterruptedException
 
final void join (long millis, int nanos) throws InterruptedException
 
void run ()
 
final void setName (String threadName)
 
final native void setPriority (int priority)
 
void setUncaughtExceptionHandler (UncaughtExceptionHandler handler)
 
synchronized native void start ()
 
String toString ()
 

Static Public Member Functions

static native int activeCount ()
 
static native Thread currentThread ()
 
static void dumpStack ()
 
static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler ()
 
static native boolean interrupted ()
 
static void setDefaultUncaughtExceptionHandler (UncaughtExceptionHandler handler)
 
static void sleep (long time) throws InterruptedException
 
static native void sleep (long millis, int nanos) throws InterruptedException
 
static native void yield ()
 
static native boolean holdsLock (Object object)
 

Static Public Attributes

static final int STATE_NEW = 0
 
static final int STATE_RUNNABLE = 1
 
static final int STATE_BLOCKED = 2
 
static final int STATE_WAITING = 3
 
static final int STATE_TIMED_WAITING = 4
 
static final int STATE_TERMINATED = 5
 
static final int MAX_PRIORITY = 10
 
static final int MIN_PRIORITY = 1
 
static final int NORM_PRIORITY = 5
 

Detailed Description

A

is a concurrent unit of execution. It has its own call stack for methods being invoked, their arguments and local variables. Each virtual machine instance has at least one main

running when it is started; typically, there are several others for housekeeping. The application might decide to launch additional

s for specific purposes.

s in the same VM interact and synchronize by the use of shared objects and monitors associated with these objects. Synchronized methods and part of the API in Object also allow

s to cooperate.

There are basically two main ways of having a

execute application code. One is providing a new class that extends

and overriding its run() method. The other is providing a new

instance with a Runnable object during its creation. In both cases, the start() method must be called to actually execute the new

.

Each

has an integer priority that basically determines the amount of CPU time the

gets. It can be set using the setPriority(int) method. A

can also be made a daemon, which makes it run in the background. The latter also affects VM termination behavior: the VM does not terminate automatically as long as there are non-daemon threads running.

See also
java.lang.Object
java.lang.ThreadGroup

Constructor & Destructor Documentation

◆ Thread() [1/4]

java.lang.Thread.Thread ( )
inline

Constructs a new

with no

Runnable

object and a newly generated name. The new

will belong to the same

ThreadGroup

as the

calling this constructor.

See also
java.lang.ThreadGroup
java.lang.Runnable

◆ Thread() [2/4]

java.lang.Thread.Thread ( Runnable  runnable)
inline

Constructs a new

with a

Runnable

object and a newly generated name. The new

will belong to the same

ThreadGroup

as the

calling this constructor.

Parameters
runnablea
Runnable
whose method run will be executed by the new
See also
java.lang.ThreadGroup
java.lang.Runnable

◆ Thread() [3/4]

java.lang.Thread.Thread ( Runnable  runnable,
String  threadName 
)
inline

Constructs a new

with a

Runnable

object and name provided. The new

will belong to the same

ThreadGroup

as the

calling this constructor.

Parameters
runnablea
Runnable
whose method run will be executed by the new
threadNamethe name for the being created
See also
java.lang.ThreadGroup
java.lang.Runnable

◆ Thread() [4/4]

java.lang.Thread.Thread ( String  threadName)
inline

Constructs a new

with no

Runnable

object and the name provided. The new

will belong to the same

ThreadGroup

as the

calling this constructor.

Parameters
threadNamethe name for the being created
See also
java.lang.ThreadGroup
java.lang.Runnable

Member Function Documentation

◆ activeCount()

static native int java.lang.Thread.activeCount ( )
static

Returns the number of active

s in the running

's group and its subgroups.

Returns
the number of s

◆ currentThread()

static native Thread java.lang.Thread.currentThread ( )
static

Returns the Thread of the caller, that is, the current Thread.

Returns
the current Thread.

◆ dumpStack()

static void java.lang.Thread.dumpStack ( )
inlinestatic

Prints to the standard error stream a text representation of the current stack for this Thread.

See also
Throwable::printStackTrace()

◆ getDefaultUncaughtExceptionHandler()

static UncaughtExceptionHandler java.lang.Thread.getDefaultUncaughtExceptionHandler ( )
inlinestatic

Returns the default exception handler that's executed when uncaught exception terminates a thread.

Returns
an UncaughtExceptionHandler or null if none exists.

◆ getId()

long java.lang.Thread.getId ( )
inline

Returns the thread's identifier. The ID is a positive long generated on thread creation, is unique to the thread, and doesn't change during the lifetime of the thread; the ID may be reused after the thread has been terminated.

Returns
the thread's ID.

◆ getName()

final String java.lang.Thread.getName ( )
inline

Returns the name of the Thread.

Returns
the Thread's name

◆ getPriority()

final int java.lang.Thread.getPriority ( )
inline

Returns the priority of the Thread.

Returns
the Thread's priority
See also
Thread::setPriority

◆ getUncaughtExceptionHandler()

UncaughtExceptionHandler java.lang.Thread.getUncaughtExceptionHandler ( )
inline

Returns the thread's uncaught exception handler. If not explicitly set, then the ThreadGroup's handler is returned. If the thread is terminated, then null is returned.

Returns
an UncaughtExceptionHandler instance or
null
.

◆ holdsLock()

static native boolean java.lang.Thread.holdsLock ( Object  object)
static

Indicates whether the current Thread has a monitor lock on the specified object.

Parameters
objectthe object to test for the monitor lock
Returns
true if the current thread has a monitor lock on the specified object; false otherwise

◆ interrupt()

native void java.lang.Thread.interrupt ( )

Posts an interrupt request to this

. The behavior depends on the state of this

:

◆ interrupted()

static native boolean java.lang.Thread.interrupted ( )
static

Returns a boolean indicating whether the current Thread ( currentThread()) has a pending interrupt request ( true) or not (false). It also has the side-effect of clearing the flag.

Returns
a boolean indicating the interrupt status
See also
Thread::currentThread
Thread::interrupt
Thread::isInterrupted

◆ isAlive()

final native boolean java.lang.Thread.isAlive ( )

Returns true if the receiver has already been started and still runs code (hasn't died yet). Returns false either if the receiver hasn't been started yet or if it has already started and run to completion and died.

Returns
a boolean indicating the liveness of the Thread
See also
Thread::start

◆ isInterrupted()

native boolean java.lang.Thread.isInterrupted ( )

Returns a boolean indicating whether the receiver has a pending interrupt request (true) or not ( false)

Returns
a boolean indicating the interrupt status
See also
Thread::interrupt
Thread::interrupted

◆ join() [1/3]

final void java.lang.Thread.join ( ) throws InterruptedException
inline

Blocks the current Thread (Thread.currentThread()) until the receiver finishes its execution and dies.

Exceptions
InterruptedExceptionif interrupt() was called for the receiver while it was in the join() call
See also
Object::notifyAll
java.lang.ThreadDeath

◆ join() [2/3]

final void java.lang.Thread.join ( long  millis) throws InterruptedException
inline

Blocks the current Thread (Thread.currentThread()) until the receiver finishes its execution and dies or the specified timeout expires, whatever happens first.

Parameters
millisThe maximum time to wait (in milliseconds).
Exceptions
InterruptedExceptionif interrupt() was called for the receiver while it was in the join() call
See also
Object::notifyAll
java.lang.ThreadDeath

◆ join() [3/3]

final void java.lang.Thread.join ( long  millis,
int  nanos 
) throws InterruptedException
inline

Blocks the current Thread (Thread.currentThread()) until the receiver finishes its execution and dies or the specified timeout expires, whatever happens first.

Parameters
millisThe maximum time to wait (in milliseconds).
nanosExtra nanosecond precision
Exceptions
InterruptedExceptionif interrupt() was called for the receiver while it was in the join() call
See also
Object::notifyAll
java.lang.ThreadDeath

◆ run()

void java.lang.Thread.run ( )
inline

Calls the run() method of the Runnable object the receiver holds. If no Runnable is set, does nothing.

See also
Thread::start

Implements java.lang.Runnable.

◆ setDefaultUncaughtExceptionHandler()

static void java.lang.Thread.setDefaultUncaughtExceptionHandler ( UncaughtExceptionHandler  handler)
inlinestatic

Sets the default uncaught exception handler. This handler is invoked in case any Thread dies due to an unhandled exception.

Parameters
handlerThe handler to set or null.

◆ setName()

final void java.lang.Thread.setName ( String  threadName)
inline

Sets the name of the Thread.

Parameters
threadNamethe new name for the Thread
See also
Thread::getName

◆ setPriority()

final native void java.lang.Thread.setPriority ( int  priority)

Sets the priority of the Thread. Note that the final priority set may not be the parameter that was passed - it will depend on the receiver's ThreadGroup. The priority cannot be set to be higher than the receiver's ThreadGroup's maxPriority().

Parameters
prioritynew priority for the Thread
Exceptions
IllegalArgumentExceptionif the new priority is greater than Thread.MAX_PRIORITY or less than Thread.MIN_PRIORITY
See also
Thread::getPriority

◆ setUncaughtExceptionHandler()

void java.lang.Thread.setUncaughtExceptionHandler ( UncaughtExceptionHandler  handler)
inline

Sets the uncaught exception handler. This handler is invoked in case this Thread dies due to an unhandled exception.

Parameters
handlerThe handler to set or null.

◆ sleep() [1/2]

static void java.lang.Thread.sleep ( long  time) throws InterruptedException
inlinestatic

Causes the thread which sent this message to sleep for the given interval of time (given in milliseconds). The precision is not guaranteed - the Thread may sleep more or less than requested.

Parameters
timeThe time to sleep in milliseconds.
Exceptions
InterruptedExceptionif interrupt() was called for this Thread while it was sleeping
See also
Thread::interrupt()

◆ sleep() [2/2]

static native void java.lang.Thread.sleep ( long  millis,
int  nanos 
) throws InterruptedException
static

Causes the thread which sent this message to sleep for the given interval of time (given in milliseconds and nanoseconds). The precision is not guaranteed - the Thread may sleep more or less than requested.

Parameters
millisThe time to sleep in milliseconds.
nanosExtra nanosecond precision
Exceptions
InterruptedExceptionif interrupt() was called for this Thread while it was sleeping
See also
Thread::interrupt()

◆ start()

synchronized native void java.lang.Thread.start ( )

Starts the new Thread of execution. The run() method of the receiver will be called by the receiver Thread itself (and not the Thread calling start()).

Exceptions
IllegalThreadStateExceptionif the Thread has been started before
See also
Thread::run

◆ toString()

String java.lang.Thread.toString ( )
inline

Returns a string containing a concise, human-readable description of the Thread. It includes the Thread's name, priority, and group name.

Returns
a printable representation for the receiver.

◆ yield()

static native void java.lang.Thread.yield ( )
static

Causes the calling Thread to yield execution time to another Thread that is ready to run. The actual scheduling is implementation-dependent.

Member Data Documentation

◆ MAX_PRIORITY

final int java.lang.Thread.MAX_PRIORITY = 10
static

The maximum priority value allowed for a thread.

◆ MIN_PRIORITY

final int java.lang.Thread.MIN_PRIORITY = 1
static

The minimum priority value allowed for a thread.

◆ NORM_PRIORITY

final int java.lang.Thread.NORM_PRIORITY = 5
static

The normal (default) priority value assigned to threads.

◆ STATE_BLOCKED

final int java.lang.Thread.STATE_BLOCKED = 2
static

The thread is blocked and waiting for a lock.

◆ STATE_NEW

final int java.lang.Thread.STATE_NEW = 0
static

The thread has been created, but has never been started.

◆ STATE_RUNNABLE

final int java.lang.Thread.STATE_RUNNABLE = 1
static

The thread may be run.

◆ STATE_TERMINATED

final int java.lang.Thread.STATE_TERMINATED = 5
static

The thread has been terminated.

◆ STATE_TIMED_WAITING

final int java.lang.Thread.STATE_TIMED_WAITING = 4
static

The thread is waiting for a specified amount of time.

◆ STATE_WAITING

final int java.lang.Thread.STATE_WAITING = 3
static

The thread is waiting.


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