|
Build 1.1_r1 (from source) | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.lang.Thread
public class Thread
A Thread 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 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 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 Threads to cooperate.
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. A Thread 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.
Object,
ThreadGroup| Nested Class Summary | |
|---|---|
static class |
Thread.State
A representation of a thread's state. |
static interface |
Thread.UncaughtExceptionHandler
Implemented by objects that want to handle cases where a thread is being terminated by an uncaught exception. |
| Field Summary | |
|---|---|
(package private) boolean |
daemon
|
(package private) ThreadGroup |
group
|
(package private) boolean |
hasBeenStarted
Reflects whether this Thread has already been started. |
(package private) ThreadLocal.Values |
inheritableValues
Inheritable thread local values. |
(package private) ThreadLocal.Values |
localValues
Normal thread local values. |
static int |
MAX_PRIORITY
The maximum priority value allowed for a thread. |
static int |
MIN_PRIORITY
The minimum priority value allowed for a thread. |
(package private) String |
name
|
static int |
NORM_PRIORITY
The normal (default) priority value assigned to threads. |
(package private) int |
priority
|
(package private) long |
stackSize
|
(package private) Runnable |
target
|
(package private) VMThread |
vmThread
|
| Constructor Summary | |
|---|---|
Thread()
Constructs a new Thread with no Runnable object and a
newly generated name. |
|
Thread(Runnable runnable)
Constructs a new Thread with a Runnable object and a
newly generated name. |
|
Thread(Runnable runnable,
String threadName)
Constructs a new Thread with a Runnable object and name
provided. |
|
Thread(String threadName)
Constructs a new Thread with no Runnable object and the
name provided. |
|
Thread(ThreadGroup group,
Runnable runnable)
Constructs a new Thread with a Runnable object and a
newly generated name. |
|
Thread(ThreadGroup group,
Runnable runnable,
String threadName)
Constructs a new Thread with a Runnable object, the given
name and belonging to the ThreadGroup passed as parameter. |
|
Thread(ThreadGroup group,
Runnable runnable,
String threadName,
long stackSize)
Constructs a new Thread with a Runnable object, the given
name and belonging to the ThreadGroup passed as parameter. |
|
Thread(ThreadGroup group,
String threadName)
Constructs a new Thread with no Runnable object, the
given name and belonging to the ThreadGroup passed as parameter. |
|
Thread(ThreadGroup group,
String name,
int priority,
boolean daemon)
Package-scope method invoked by Dalvik VM to create "internal" threads or attach threads created externally. |
|
| Method Summary | |
|---|---|
static int |
activeCount()
Returns the number of active Threads in the running Thread's group and its subgroups. |
void |
checkAccess()
Is used for operations that require approval from a SecurityManager. |
int |
countStackFrames()
Deprecated. The results of this call were never well defined. To make things worse, it would depend on whether the Thread was suspended or not, and suspend was deprecated too. |
static Thread |
currentThread()
Returns the Thread of the caller, that is, the current Thread. |
void |
destroy()
Deprecated. Not implemented. |
static void |
dumpStack()
Prints to the standard error stream a text representation of the current stack for this Thread. |
static int |
enumerate(Thread[] threads)
Copies an array with all Threads which are in the same ThreadGroup as the receiver - and subgroups - into the array threads passed as
parameter. |
static Map<Thread,StackTraceElement[]> |
getAllStackTraces()
Returns the stack traces of all the currently live threads and puts them into the given map. |
ClassLoader |
getContextClassLoader()
Returns the context ClassLoader for this Thread. |
static Thread.UncaughtExceptionHandler |
getDefaultUncaughtExceptionHandler()
Returns the default exception handler that's executed when uncaught exception terminates a thread. |
long |
getId()
Returns the thread's identifier. |
String |
getName()
Returns the name of the Thread. |
int |
getPriority()
Returns the priority of the Thread. |
StackTraceElement[] |
getStackTrace()
Returns the a stack trace representing the current execution state of this Thread. |
Thread.State |
getState()
Returns the current state of the Thread. |
ThreadGroup |
getThreadGroup()
Returns the ThreadGroup to which this Thread belongs. |
Thread.UncaughtExceptionHandler |
getUncaughtExceptionHandler()
Returns the thread's uncaught exception handler. |
static boolean |
holdsLock(Object object)
Indicates whether the current Thread has a monitor lock on the specified object. |
void |
interrupt()
Posts an interrupt request to this Thread. |
static boolean |
interrupted()
Returns a boolean indicating whether the current Thread (
currentThread()) has a pending interrupt request (
true) or not (false). |
boolean |
isAlive()
Returns true if the receiver has already been started and
still runs code (hasn't died yet). |
boolean |
isDaemon()
Returns a boolean indicating whether the receiver is a
daemon Thread (true) or not (false) A
daemon Thread only runs as long as there are non-daemon Threads running. |
boolean |
isInterrupted()
Returns a boolean indicating whether the receiver has a
pending interrupt request (true) or not (
false) |
void |
join()
Blocks the current Thread ( Thread.currentThread()) until
the receiver finishes its execution and dies. |
void |
join(long millis)
Blocks the current Thread ( Thread.currentThread()) until
the receiver finishes its execution and dies or the specified timeout
expires, whatever happens first. |
void |
join(long millis,
int nanos)
Blocks the current Thread ( Thread.currentThread()) until
the receiver finishes its execution and dies or the specified timeout
expires, whatever happens first. |
(package private) void |
parkFor(long nanos)
Implementation of parkFor(). |
(package private) void |
parkUntil(long time)
Implementation of parkUntil(). |
void |
resume()
Deprecated. Used with deprecated method suspend() |
void |
run()
Calls the run() method of the Runnable object the receiver
holds. |
void |
setContextClassLoader(ClassLoader cl)
Set the context ClassLoader for the receiver. |
void |
setDaemon(boolean isDaemon)
Set if the receiver is a daemon Thread or not. |
static void |
setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
Sets the default uncaught exception handler. |
void |
setName(String threadName)
Sets the name of the Thread. |
void |
setPriority(int priority)
Sets the priority of the Thread. |
void |
setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
Sets the uncaught exception handler. |
static void |
sleep(long time)
Causes the thread which sent this message to sleep for the given interval of time (given in milliseconds). |
static void |
sleep(long millis,
int nanos)
Causes the thread which sent this message to sleep for the given interval of time (given in milliseconds and nanoseconds). |
void |
start()
Starts the new Thread of execution. |
void |
stop()
Deprecated. because stopping a thread in this manner is unsafe and can leave your application and the VM in an unpredictable state. |
void |
stop(Throwable throwable)
Deprecated. because stopping a thread in this manner is unsafe and can leave your application and the VM in an unpredictable state. |
void |
suspend()
Deprecated. May cause deadlocks. |
String |
toString()
Returns a string containing a concise, human-readable description of the Thread. |
(package private) void |
unpark()
Implementation of unpark(). |
static void |
yield()
Causes the calling Thread to yield execution time to another Thread that is ready to run. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final int MAX_PRIORITY
public static final int MIN_PRIORITY
public static final int NORM_PRIORITY
volatile VMThread vmThread
volatile ThreadGroup group
volatile boolean daemon
volatile String name
volatile int priority
volatile long stackSize
Runnable target
ThreadLocal.Values localValues
ThreadLocal.Values inheritableValues
boolean hasBeenStarted
| Constructor Detail |
|---|
public Thread()
Thread with no Runnable object and a
newly generated name. The new Thread will belong to the same
ThreadGroup as the Thread calling this constructor.
ThreadGroup,
Runnablepublic Thread(Runnable runnable)
Thread with a Runnable object and a
newly generated name. The new Thread will belong to the same
ThreadGroup as the Thread calling this constructor.
runnable - a Runnable whose method run will be
executed by the new ThreadThreadGroup,
Runnable
public Thread(Runnable runnable,
String threadName)
Thread with a Runnable object and name
provided. The new Thread will belong to the same ThreadGroup as the Thread calling this constructor.
runnable - a Runnable whose method run will be
executed by the new ThreadthreadName - the name for the Thread being createdThreadGroup,
Runnablepublic Thread(String threadName)
Thread with no Runnable object and the
name provided. The new Thread will belong to the same ThreadGroup as the Thread calling this constructor.
threadName - the name for the Thread being createdThreadGroup,
Runnable
public Thread(ThreadGroup group,
Runnable runnable)
Thread with a Runnable object and a
newly generated name. The new Thread will belong to the ThreadGroup passed as parameter.
group - ThreadGroup to which the new Thread will
belongrunnable - a Runnable whose method run will be
executed by the new Thread
SecurityException - if group.checkAccess() fails with a
SecurityException
IllegalThreadStateException - if group.destroy() has already been doneThreadGroup,
Runnable,
SecurityException,
SecurityManager
public Thread(ThreadGroup group,
Runnable runnable,
String threadName)
Thread with a Runnable object, the given
name and belonging to the ThreadGroup passed as parameter.
group - ThreadGroup to which the new Thread will belongrunnable - a Runnable whose method run will be
executed by the new ThreadthreadName - the name for the Thread being created
SecurityException - if group.checkAccess() fails with a
SecurityException
IllegalThreadStateException - if group.destroy() has already been doneThreadGroup,
Runnable,
SecurityException,
SecurityManager
public Thread(ThreadGroup group,
String threadName)
Thread with no Runnable object, the
given name and belonging to the ThreadGroup passed as parameter.
group - ThreadGroup to which the new Thread will belongthreadName - the name for the Thread being created
SecurityException - if group.checkAccess() fails with a
SecurityException
IllegalThreadStateException - if group.destroy() has already been doneThreadGroup,
Runnable,
SecurityException,
SecurityManager
public Thread(ThreadGroup group,
Runnable runnable,
String threadName,
long stackSize)
Thread with a Runnable object, the given
name and belonging to the ThreadGroup passed as parameter.
group - ThreadGroup to which the new Thread will
belongrunnable - a Runnable whose method run will be
executed by the new ThreadthreadName - the name for the Thread being createdstackSize - a stack size for the new Thread. This has a highly
platform-dependent interpretation. It may even be ignored
completely.
SecurityException - if group.checkAccess() fails with a
SecurityException
IllegalThreadStateException - if group.destroy() has already been doneThreadGroup,
Runnable,
SecurityException,
SecurityManager
Thread(ThreadGroup group,
String name,
int priority,
boolean daemon)
| Method Detail |
|---|
public static int activeCount()
Threads in the running Thread's group and its subgroups.
Threadspublic final void checkAccess()
SecurityManager.checkAccess(Thread) is
called for that SecurityManager.
SecurityException - if a SecurityManager is installed and it does not allow
access to the Thread.SecurityException,
SecurityManager@Deprecated public int countStackFrames()
public static Thread currentThread()
@Deprecated public void destroy()
public static void dumpStack()
Throwable.printStackTrace()public static int enumerate(Thread[] threads)
threads passed as
parameter. If the array passed as parameter is too small no exception is
thrown - the extra elements are simply not copied.
threads - array into which the Threads will be copied
SecurityException - if the installed SecurityManager fails
SecurityManager.checkAccess(Thread)SecurityException,
SecurityManagerpublic static Map<Thread,StackTraceElement[]> getAllStackTraces()
Returns the stack traces of all the currently live threads and puts them into the given map.
SecurityException - if the current SecurityManager fails the
SecurityManager.checkPermission(java.security.Permission)
call.public ClassLoader getContextClassLoader()
If the conditions
RuntimePermission("getClassLoader") is performed first.
SecurityException - if the aforementioned security check fails.ClassLoader,
getContextClassLoader()public static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()
Thread.UncaughtExceptionHandler or null if
none exists.public long getId()
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.
public final String getName()
public final int getPriority()
setPriority(int)public StackTraceElement[] getStackTrace()
The RuntimePermission("getStackTrace") is checked before
returning a result.
SecurityException - if the current SecurityManager fails the
SecurityManager.checkPermission(java.security.Permission)
call.public Thread.State getState()
Thread.State value.public final ThreadGroup getThreadGroup()
public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
null is returned.
Thread.UncaughtExceptionHandler instance or null.public void interrupt()
Thread. Unless the caller is
the currentThread(), the method checkAccess() is called
for the installed SecurityManager, if any. This may result in a
SecurityException being thrown. The further behavior depends on
the state of this Thread:
Threads blocked in one of Object's wait() methods
or one of Thread's join() or sleep() methods will
be woken up, their interrupt status will be cleared, and they receive an
InterruptedException.
Threads blocked in an I/O operation of an
InterruptibleChannel will have their interrupt
status set and receive an
ClosedByInterruptException. Also, the channel
will be closed.
Threads blocked in a Selector will have
their interrupt status set and return immediately. They don't receive an
exception in this case.
SecurityException - if checkAccess() fails with a SecurityExceptionSecurityException,
SecurityManager,
interrupted(),
isInterrupted()public static boolean interrupted()
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.
boolean indicating the interrupt statuscurrentThread(),
interrupt(),
isInterrupted()public final boolean isAlive()
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.
boolean indicating the lifeness of the Threadstart()public final boolean isDaemon()
boolean indicating whether the receiver is a
daemon Thread (true) or not (false) A
daemon Thread only runs as long as there are non-daemon Threads running.
When the last non-daemon Thread ends, the whole program ends no matter if
it had daemon Threads still running or not.
boolean indicating whether the Thread is a daemonsetDaemon(boolean)public boolean isInterrupted()
boolean indicating whether the receiver has a
pending interrupt request (true) or not (
false)
boolean indicating the interrupt statusinterrupt(),
interrupted()
public final void join()
throws InterruptedException
Thread.currentThread()) until
the receiver finishes its execution and dies.
InterruptedException - if interrupt() was called for
the receiver while it was in the join() callObject.notifyAll(),
ThreadDeath
public final void join(long millis)
throws InterruptedException
Thread.currentThread()) until
the receiver finishes its execution and dies or the specified timeout
expires, whatever happens first.
millis - The maximum time to wait (in milliseconds).
InterruptedException - if interrupt() was called for
the receiver while it was in the join() callObject.notifyAll(),
ThreadDeath
public final void join(long millis,
int nanos)
throws InterruptedException
Thread.currentThread()) until
the receiver finishes its execution and dies or the specified timeout
expires, whatever happens first.
millis - The maximum time to wait (in milliseconds).nanos - Extra nanosecond precision
InterruptedException - if interrupt() was called for
the receiver while it was in the join() callObject.notifyAll(),
ThreadDeath@Deprecated public final void resume()
suspend()
SecurityException - if checkAccess() fails with a SecurityExceptionsuspend()public void run()
run() method of the Runnable object the receiver
holds. If no Runnable is set, does nothing.
run in interface Runnablestart()public void setContextClassLoader(ClassLoader cl)
The RuntimePermission("setContextClassLoader")
is checked prior to setting the handler.
cl - The context ClassLoader
SecurityException - if the current SecurityManager fails the
checkPermission call.ClassLoader,
getContextClassLoader()public final void setDaemon(boolean isDaemon)
isDaemon - indicates whether the Thread should be daemon or not
SecurityException - if checkAccess() fails with a SecurityExceptionisDaemon()public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
Sets the default uncaught exception handler. This handler is invoked in case any Thread dies due to an unhandled exception.
The RuntimePermission("setDefaultUncaughtExceptionHandler")
is checked prior to setting the handler.
handler - The handler to set or null.
SecurityException - if the current SecurityManager fails the checkPermission
call.public final void setName(String threadName)
threadName - the new name for the Thread
SecurityException - if checkAccess() fails with a
SecurityExceptiongetName()public final void setPriority(int priority)
priority - new priority for the Thread
SecurityException - if checkAccess() fails with a SecurityException
IllegalArgumentException - if the new priority is greater than Thread.MAX_PRIORITY or
less than Thread.MIN_PRIORITYgetPriority()public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
Sets the uncaught exception handler. This handler is invoked in case this Thread dies due to an unhandled exception.
handler - The handler to set or null.
SecurityException - if the current SecurityManager fails the checkAccess call.
public static void sleep(long time)
throws InterruptedException
time - The time to sleep in milliseconds.
InterruptedException - if interrupt() was called for this Thread while
it was sleepinginterrupt()
public static void sleep(long millis,
int nanos)
throws InterruptedException
millis - The time to sleep in milliseconds.nanos - Extra nanosecond precision
InterruptedException - if interrupt() was called for this Thread while
it was sleepinginterrupt()public void start()
run() method of
the receiver will be called by the receiver Thread itself (and not the
Thread calling start()).
IllegalThreadStateException - if the Thread has been started beforerun()@Deprecated public final void stop()
SecurityException - if checkAccess() fails with a
SecurityException@Deprecated public final void stop(Throwable throwable)
throwable(). The Thread is resumed if it was suspended
and awakened if it was sleeping, so that it can proceed to throw the
throwable().
throwable - Throwable object to be thrown by the Thread
SecurityException - if checkAccess() fails with a
SecurityException
NullPointerException - if throwable() is
null@Deprecated public final void suspend()
isAlive() however, suspended it until
resume() is sent to it. Suspend requests are not queued, which
means that N requests are equivalent to just one - only one resume
request is needed in this case.
SecurityException - if checkAccess() fails with a SecurityExceptionresume()public String toString()
toString in class Objectpublic static void yield()
public static boolean holdsLock(Object object)
object - the object to test for the monitor lock
void unpark()
unpark(). See LangAccessImpl.
void parkFor(long nanos)
parkFor(). See LangAccessImpl.
This method must only be called when this is the current
thread.
nanos - number of nanoseconds to park forvoid parkUntil(long time)
parkUntil(). See LangAccessImpl.
This method must only be called when this is the current
thread.
time - absolute milliseconds since the epoch to park until
|
Build 1.1_r1 (from source) | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||