Thread - JRE Emulation | JRE Emulation
public class

Thread

extends Object
implements Runnable
java.lang.Object
   ↳ java.lang.Thread
Known Direct Subclasses

Class Overview

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.

Summary

Nested Classes
enum Thread.State A representation of a thread's state. 
interface Thread.UncaughtExceptionHandler  
Constants
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 Constructors
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, 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 Methods
static int activeCount()
void checkAccess()
int countStackFrames()
static Thread currentThread()
void destroy()
static void dumpStack()
Prints to the standard error stream a text representation of the current stack for this Thread.
static int enumerate(Thread[] threads)
static Map<ThreadStackTraceElement[]> 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 getPriority()
StackTraceElement[] getStackTrace()
Thread.State getState()
ThreadGroup getThreadGroup()
Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
Returns the handler invoked when this thread abruptly terminates due to an uncaught exception.
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).
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 setContextClassLoader(ClassLoader cl)
void setDaemon(boolean isDaemon)
static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
Sets the default uncaught exception handler.
final void setName(String name)
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 stop(Throwable obj)
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 Methods
[Expand]
From class java.lang.Object
From interface java.lang.Runnable

Constants

public static final int MAX_PRIORITY

The maximum priority value allowed for a thread.

Constant Value: 10 (0x0000000a)

public static final int MIN_PRIORITY

The minimum priority value allowed for a thread.

Constant Value: 1 (0x00000001)

public static final int NORM_PRIORITY

The normal (default) priority value assigned to threads.

Constant Value: 5 (0x00000005)

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

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

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

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 checkAccess ()

public int countStackFrames ()

public static Thread currentThread ()

public void destroy ()

public static void dumpStack ()

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

public static int enumerate (Thread[] threads)

public static Map<ThreadStackTraceElement[]> 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

public long getId ()

public final String getName ()

public final int getPriority ()

public StackTraceElement[] getStackTrace ()

public Thread.State getState ()

public ThreadGroup getThreadGroup ()

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:

  • 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.

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

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

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

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

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 setContextClassLoader (ClassLoader cl)

public void setDaemon (boolean 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 setName (String name)

public final void setPriority (int 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 static void sleep (long millis)

public synchronized void start ()

public final void stop ()

public final void stop (Throwable obj)

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.