Known Direct Subclasses |
Simplified iOS version of java.lang.Thread, based on Apache Harmony source (both luni-kernel and vmcore). This class uses pthread for thread creation and maintains a pthread handle for using other pthread functionality. pthread's thread local mechanism (pthread_setspecific) is used to associate this wrapper object with the current thread.
Nested Class Summary
enum | Thread.State | A representation of a thread's state. | |
interface | Thread.UncaughtExceptionHandler |
Constant Summary
int | MAX_PRIORITY | The maximum priority value allowed for a thread. |
int | MIN_PRIORITY | The minimum priority value allowed for a thread. |
int | NORM_PRIORITY | The normal (default) priority value assigned to threads. |
Public Constructor Summary
Thread()
Constructs a new Thread with no runnable object and a newly generated
name.
|
|
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, long stack)
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)
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.
|
Public Method Summary
static int | |
void |
blockedOn(Interruptible b)
Set the IOBlocker field; invoked from java.nio code.
|
void | |
int | |
static Thread | |
void |
destroy()
|
static void |
dumpStack()
Prints to the standard error stream a text representation of the current
stack for this Thread.
|
static int | |
static Map<Thread, StackTraceElement[]> |
getAllStackTraces()
Returns a map of stack traces for all live threads.
|
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()
|
final String |
getName()
|
final int | |
StackTraceElement[] | |
Thread.State |
getState()
|
ThreadGroup | |
Thread.UncaughtExceptionHandler |
getUncaughtExceptionHandler()
Returns the handler invoked when this thread abruptly terminates
due to an uncaught exception.
|
static boolean | |
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 ). |
final boolean |
isAlive()
|
boolean |
isDaemon()
|
boolean |
isInterrupted()
Returns a
boolean indicating whether the receiver has a
pending interrupt request (true ) or not (
false ) |
final void |
join()
Blocks the current Thread (
Thread.currentThread() ) until
the receiver finishes its execution and dies. |
final 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. |
final 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. |
final void |
resume()
|
void |
run()
When an object implementing interface
Runnable is used
to create a thread, starting the thread causes the object's
run method to be called in that separately executing
thread. |
void | |
void |
setDaemon(boolean isDaemon)
|
static void |
setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
Sets the default uncaught exception handler.
|
final void | |
final void |
setPriority(int newPriority)
|
void |
setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)
Set the handler invoked when this thread abruptly terminates
due to an uncaught exception.
|
static void |
sleep(long millis, int nanos)
|
static void |
sleep(long millis)
|
synchronized void |
start()
|
final void |
stop()
|
final void | |
final void |
suspend()
|
String |
toString()
Returns a string containing a concise, human-readable description of this
object.
|
static void |
yield()
Causes the calling Thread to yield execution time to another Thread that
is ready to run.
|
Inherited Method Summary
Constants
public static final int MAX_PRIORITY
The maximum priority value allowed for a thread.
public static final int MIN_PRIORITY
The minimum priority value allowed for a thread.
public static final int NORM_PRIORITY
The normal (default) priority value assigned to threads.
Public Constructors
public Thread ()
Constructs a new 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.
See Also
public Thread (Runnable runnable)
Constructs a new 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.
Parameters
runnable | a java.lang.Runnable whose method run will
be executed by the new Thread |
---|
See Also
public Thread (Runnable runnable, String threadName)
Constructs a new Thread with a runnable object and name provided. The new Thread will belong to the same ThreadGroup as the Thread calling this constructor.
Parameters
runnable | a java.lang.Runnable whose method run will
be executed by the new Thread |
---|---|
threadName | Name for the Thread being created |
See Also
public Thread (String threadName)
Constructs a new Thread with no runnable object and the name provided. The new Thread will belong to the same ThreadGroup as the Thread calling this constructor.
Parameters
threadName | Name for the Thread being created |
---|
See Also
public Thread (ThreadGroup group, Runnable runnable)
Constructs a new Thread with a runnable object and a newly generated name. The new Thread will belong to the ThreadGroup passed as parameter.
Parameters
group | ThreadGroup to which the new Thread will belong |
---|---|
runnable | a java.lang.Runnable whose method run will
be executed by the new Thread |
Throws
SecurityException | if group.checkAccess() fails
with a SecurityException |
---|---|
IllegalThreadStateException | if group.destroy() has
already been done |
public Thread (ThreadGroup group, Runnable runnable, String threadName, long stack)
Constructs a new Thread with a runnable object, the given name and belonging to the ThreadGroup passed as parameter.
Parameters
group | ThreadGroup to which the new Thread will belong |
---|---|
runnable | a java.lang.Runnable whose method run will
be executed by the new Thread |
threadName | Name for the Thread being created |
stack | Platform dependent stack size |
Throws
SecurityException | if group.checkAccess() fails
with a SecurityException |
---|---|
IllegalThreadStateException | if group.destroy() has
already been done |
public 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.
Parameters
group | ThreadGroup to which the new Thread will belong |
---|---|
runnable | a java.lang.Runnable whose method run will
be executed by the new Thread |
threadName | Name for the Thread being created |
Throws
SecurityException | if group.checkAccess() fails
with a SecurityException |
---|---|
IllegalThreadStateException | if group.destroy() has
already been done |
public Thread (ThreadGroup group, String threadName)
Constructs a new Thread with no runnable object, the given name and belonging to the ThreadGroup passed as parameter.
Parameters
group | ThreadGroup to which the new Thread will belong |
---|---|
threadName | Name for the Thread being created |
Throws
SecurityException | if group.checkAccess() fails
with a SecurityException |
---|---|
IllegalThreadStateException | if group.destroy() has
already been done |
Public Methods
public static int activeCount ()
public void blockedOn (Interruptible b)
Set the IOBlocker field; invoked from java.nio code.
Parameters
b |
---|
public void checkAccess ()
public int countStackFrames ()
public void destroy ()
public static void dumpStack ()
Prints to the standard error stream a text representation of the current stack for this Thread.
See Also
public static Map<Thread, StackTraceElement[]> getAllStackTraces ()
Returns a map of stack traces for all live threads.
public ClassLoader getContextClassLoader ()
Returns the context ClassLoader for this Thread.
Returns
- ClassLoader The context ClassLoader
See Also
public static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler ()
Returns the default exception handler that's executed when uncaught exception terminates a thread.
Returns
- an
Thread.UncaughtExceptionHandler
ornull
if none exists.
public long getId ()
public final int getPriority ()
public Thread.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 ThreadGroup object is returned, unless this thread has terminated, in which case null is returned.
public static boolean holdsLock (Object object)
Indicates whether the current Thread has a monitor lock on the specified object.
Parameters
object | the object to test for the monitor lock |
---|
Returns
- true if the current thread has a monitor lock on the specified object; false otherwise
public void interrupt ()
Posts an interrupt request to this 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
:
-
Thread
s blocked in one ofObject
'swait()
methods or one ofThread
'sjoin()
orsleep()
methods will be woken up, their interrupt status will be cleared, and they receive anInterruptedException
. -
Thread
s blocked in an I/O operation of anInterruptibleChannel
will have their interrupt status set and receive anClosedByInterruptException
. Also, the channel will be closed. -
Thread
s blocked in aSelector
will have their interrupt status set and return immediately. They don't receive an exception in this case.
Throws
SecurityException | if checkAccess() fails with a SecurityException |
---|
public static boolean interrupted ()
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
public final boolean isAlive ()
public boolean isDaemon ()
public boolean 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
public final void join ()
Blocks the current Thread (Thread.currentThread()
) until
the receiver finishes its execution and dies.
Throws
InterruptedException | if interrupt() was called for
the receiver while it was in the join() call |
---|
See Also
public final 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.
Parameters
millis | The maximum time to wait (in milliseconds). |
---|
Throws
InterruptedException | if interrupt() was called for
the receiver while it was in the join() call |
---|
See Also
public final 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.
Parameters
millis | The maximum time to wait (in milliseconds). |
---|---|
nanos | Extra nanosecond precision |
Throws
InterruptedException | if interrupt() was called for
the receiver while it was in the join() call |
---|
See Also
public final void resume ()
public void run ()
When an object implementing interface Runnable
is used
to create a thread, starting the thread causes the object's
run
method to be called in that separately executing
thread.
The general contract of the method run
is that it may
take any action whatsoever.
public void setDaemon (boolean isDaemon)
Parameters
isDaemon |
---|
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.
Parameters
handler | The handler to set or null. |
---|
public final void setPriority (int newPriority)
Parameters
newPriority |
---|
public void setUncaughtExceptionHandler (Thread.UncaughtExceptionHandler eh)
Set the handler invoked when this thread abruptly terminates due to an uncaught exception.
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 ThreadGroup object acts as its handler.
Parameters
eh | the object to use as this thread's uncaught exception handler. If null then this thread has no explicit handler. |
---|
Throws
SecurityException | if the current thread is not allowed to modify this thread. |
---|
public static void sleep (long millis, int nanos)
public synchronized void start ()
public final void stop ()
public final void suspend ()
public String toString ()
Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:
getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toString
method
if you intend implementing your own toString
method.
Returns
- a printable representation of this object.
public static void yield ()
Causes the calling Thread to yield execution time to another Thread that is ready to run. The actual scheduling is implementation-dependent.