|
![]() |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Thread
public class Thread
Class that represents Java threads.
Nested Class Summary | |
---|---|
static class |
Thread.State
JDK 1.5 thread State |
static interface |
Thread.UncaughtExceptionHandler
Interface for handler of uncaught exception. |
Field Summary | |
---|---|
static int |
MAX_PRIORITY
maximum value a standard thread priority can have. |
static int |
MIN_PRIORITY
minimum value a standard thread priority can have. |
static int |
NORM_PRIORITY
Default priority for threads. |
Constructor Summary | |
---|---|
Thread()
Constructor to create a new thread. |
|
Thread(Runnable target)
Constructor to create a new thread. |
|
Thread(Runnable target,
String name)
Constructor to create a new thread. |
|
Thread(String name)
Constructor to create a new thread. |
|
Thread(ThreadGroup group,
Runnable target)
Constructor to create a new thread. |
|
Thread(ThreadGroup group,
Runnable target,
String name)
Constructor to create a new thread with given thread group, Runnable and name. |
|
Thread(ThreadGroup group,
Runnable target,
String name,
long stackSize)
Constructor to create a new thread with given thread group, Runnable, name and stack size. |
|
Thread(ThreadGroup group,
String name)
Constructor to create a new thread. |
Method Summary | |
---|---|
static int |
activeCount()
Return the number of active thread in the current thread's thread group. |
void |
checkAccess()
checkAccess checks if the current thread is permitted to access this thread. |
int |
countStackFrames()
Deprecated. Since this must be suspended for this operation, and suspend is deprecated, this is also deprecated. |
static Thread |
currentThread()
currentThread returns a reference to the current thread. |
void |
destroy()
Deprecated. this is inherently unsafe. |
static void |
dumpStack()
dumpStack print a stack trace for the current thread to System.err. |
static int |
enumerate(Thread[] tarray)
enumerate enumberates all active threads in the current thread's thread group. |
static Map<Thread,StackTraceElement[]> |
getAllStackTraces()
Gets the stack traces of all currently alive threads. |
ClassLoader |
getContextClassLoader()
getContextClassLoader determines the context class loader for this thread. |
static Thread.UncaughtExceptionHandler |
getDefaultUncaughtExceptionHandler()
Return the current default uncaught exception handler. |
long |
getId()
getId returns a unique identifier for this thread. |
String |
getName()
getName returns the name of this thread. |
int |
getPriority()
Return the base priority of this thread. |
StackTraceElement[] |
getStackTrace()
Gets the stack trace of this thread. |
Thread.State |
getState()
Returns the current state of this thread. |
ThreadGroup |
getThreadGroup()
Return the ThreadGroup to which this thread belongs. |
Thread.UncaughtExceptionHandler |
getUncaughtExceptionHandler()
return this thread's uncaught exception handler. |
static boolean |
holdsLock(Object obj)
Checks if the current thread is the owner of the given objects monitor. |
void |
interrupt()
interrupt interrupts this thread by throwing an InterruptedException if this thread is in wait(), sleep() or join(). |
static boolean |
interrupted()
interrupted determines the interrupted status of the current thread and clears the interrupted status. |
boolean |
isAlive()
isAlive check if this thread has been started and has not died yet. |
boolean |
isDaemon()
isDaemon return the daemon state of this thread. |
boolean |
isInterrupted()
isInterrupted determines the interrupted status of this thread. |
void |
join()
join waits for this thread to die using no timeout. |
void |
join(long millis)
join waits for this thread to die. |
void |
join(long millis,
int nanos)
join waits for this thread to die using given timeout. |
void |
resume()
Deprecated. suspend/resume is deadlock-prone since all locks held by the suspended thread will be held until the thread will be resumed. If the thread that performs the resume() locks on one of these monitors, a deadlock occurs. |
void |
run()
run contains only a call to logic.run if logic is not null and not this. |
void |
setContextClassLoader(ClassLoader cl)
setContextClassLoader sets the context class loader for this thread. |
void |
setDaemon(boolean on)
setDaemon sets the daemon state of this thread. |
static void |
setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)
Set the default handler for handling an exception that caused termination of a thread that was not equipped with an uncaught exception handler. |
void |
setName(String name)
setName sets the name of this thread to the given argument. |
void |
setPriority(int newPriority)
Set the priority of this thread. |
void |
setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)
Sets this thread's uncaught exception handler. |
static void |
sleep(long millis)
sleep stops the execution of this thread for the specified number of milliseconds. |
static void |
sleep(long millis,
int nanos)
sleep stops the execution of this thread for the specified number of milliseconds and nanoseconds. |
void |
start()
start starts execution of the new thread. |
void |
stop()
Deprecated. this is inherently unsafe. Use AsynchronouslyInterruptedException instead. |
void |
stop(Throwable obj)
Deprecated. this is inherently unsafe use interrupt() instead |
void |
suspend()
Deprecated. suspend/resume is deadlock-prone since all locks held by the suspended thread will be held until the thread will be resumed. If the thread that performs the resume() locks on one of these monitors, a deadlock occurs. |
String |
toString()
toString returns a string representation of this thread consisting of thread name, priority and thread group. |
static void |
yield()
yield does a reschedule of threads |
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
Note that RTSJ threads may have a priority that is not within MIN_PRIORITY..MAX_PRIORITY, but that is in the range javax.realtime.PriorityScheduler.getMinPriority() through getMaxPriority().
For JamaicaVM, the extended priority range of RTSJ threads may be used for normal Java threads as well.
public static final int MIN_PRIORITY
Note that RTSJ threads may have a priority that is not within MIN_PRIORITY..MAX_PRIORITY, but that is in the range javax.realtime.PriorityScheduler.getMinPriority() through getMaxPriority().
For JamaicaVM, the extended priority range of RTSJ threads may be used for normal Java threads as well.
public static final int NORM_PRIORITY
Constructor Detail |
---|
public Thread()
public Thread(Runnable target)
target
- instance of Runnable whose run method contains the
code to be executed by the new thread. null to use this as
Runnable.public Thread(ThreadGroup group, Runnable target)
group
- the thread group or null.target
- instance of Runnable whose run method contains the
code to be executed by the new thread. null to use this as
Runnable.
SecurityException
- if the current thread cannot create a
new thread using the provided thread group.public Thread(String name)
name
- the name of the new thread, must not be null.public Thread(ThreadGroup group, String name)
group
- the thread group or null.name
- the name of the new thread, must not be null.
SecurityException
- if the current thread cannot create a
new thread using the provided thread group.public Thread(Runnable target, String name)
target
- instance of Runnable whose run method contains the
code to be executed by the new thread. null to use this as
Runnable.name
- the name of the new thread, must not be null.public Thread(ThreadGroup group, Runnable target, String name)
group
- the thread group or null.target
- instance of Runnable whose run method contains the
code to be executed by the new thread. null to use this as
Runnable.name
- the name of the new thread, must not be null.public Thread(ThreadGroup group, Runnable target, String name, long stackSize)
group
- the thread group or null.target
- instance of Runnable whose run method contains the
code to be executed by the new thread. null to use this as
Runnable.name
- the name of the new thread, must not be null.stackSize
- the stack size in bytes. The interpretation is
highly platform-dependent.Method Detail |
---|
public static Thread currentThread()
public static void yield()
public static void sleep(long millis) throws InterruptedException
millis
- the time this thread should delay.
InterruptedException
- if interrupt() is called by another
thread on this thread while this thread is sleeping.
IllegalArgumentException
- if millis is negative.public static void sleep(long millis, int nanos) throws InterruptedException
millis
- the time in milliseconds this thread should delay.nanos
- the time in nanoseconds this thread should delay.
InterruptedException
- if interrupt() is called by another
thread on this thread while this thread is sleeping.
IllegalArgumentException
- if millis or nanos is negative
or nanos is larger than 999999.public void start() throws IllegalThreadStateException
IllegalThreadStateException
- if this thread has already
been started.
OutOfMemoryError
- if starting failed due to lack of memory
InternalError
- if this thread instance is allocated in
ScopedMemory
and it is not a RealtimeThread
that can run in scoped memory.public void run()
run
in interface Runnable
run()
public final void stop()
AsynchronouslyInterruptedException
instead.
For JamaicaVM, this method throws a ThreadDeath exception. Throwing of this exception is, however, delayed until the next time this thread is blocked.
SecurityException
- if a security manager is installed and
a call to checkAccess(this) causes a SecurityException.public void stop(Throwable obj)
obj
- the Throwable object to be thrown in the new
thread. Parameter ignored by JamaicaVM.
SecurityException
- if a security manager is installed and
a call to checkAccess(this) causes a SecurityException.public void interrupt() throws SecurityException
Otherwise, this thread's state will be set to interrupted.
SecurityException
- if a security manager is installed and
a call to checkAccess(this) causes a SecurityException.InterruptibleChannel
public static boolean interrupted()
public boolean isInterrupted()
public void destroy()
NoSuchMethodError.
public final boolean isAlive()
public final void suspend()
For JamaicaVM, this is implemented using the OS's low level suspend/resume mechanism.
SecurityException
- if a security manager is installed and
a call to checkAccess(this) causes a SecurityException.stop()
public final void resume()
For JamaicaVM, this is implemented using the OS's low level suspend/resume mechanism.
SecurityException
- if a security manager is installed and
a call to checkAccess(this) causes a SecurityException.stop()
public final void setPriority(int newPriority) throws SecurityException, IllegalArgumentException
newPriority
- the new priority, in the range of 1
.. javax.realtime.PriorityScheduler.instance().getMaxPriority().
ClassCastException
- if this is an instance of
RealtimeThread and this.getSchedulingParameters() is not an
instance of PriorityParameters.
IllegalArgumentException
- if the new priority is less than
MIN_PRIORITY or larger than
PriorityScheduler.instance().getMaxPriority().
SecurityException
- if a security manager is installed and
a call to checkAccess(this) causes a SecurityException.public final int getPriority()
ClassCastException
- if this is an instance of
RealtimeThread and this.getSchedulingParameters() is not an
instance of PriorityParameters.public final void setName(String name) throws SecurityException
name
- the new name. Must not be null.
SecurityException
- if a security manager is installed and
a call to checkAccess(this) causes a SecurityException.
NullPointerException
- if name is null.public final String getName()
public final ThreadGroup getThreadGroup()
public static int activeCount()
public static int enumerate(Thread[] tarray) throws SecurityException
tarray
- the target array the thread should be stored in. must not be null.
SecurityException
- if a security manager is installed and
a call to checkAccess(this) causes a SecurityException.public int countStackFrames() throws IllegalThreadStateException
IllegalThreadStateException
public final void join(long millis) throws InterruptedException
millis
- the timeout in milliseconds. 0 for no timeout.
InterruptedException
- if this thread is interrupted while
waiting.
IllegalArgumentException
- if millis is negative.public final void join(long millis, int nanos) throws InterruptedException, IllegalArgumentException
millis
- the timeout in milliseconds.nanos
- the timeout in nanoseconds.
InterruptedException
- if this thread is interrupted while
waiting.
IllegalArgumentException
- if millis is negative or nanos
is larger than 999999.public final void join() throws InterruptedException
InterruptedException
- if this thread is interrupted while
waiting.public static void dumpStack()
public final void setDaemon(boolean on) throws SecurityException, IllegalThreadStateException
on
- true to set the daemon state, false to clear it.
SecurityException
- if a security manager is installed and
a call to checkAccess(this) causes a SecurityException.
IllegalThreadStateException
public final boolean isDaemon()
public final void checkAccess() throws SecurityException
SecurityException
- if a security manager is installed and
a call to checkAccess(this) causes a SecurityException.public String toString()
toString
in class Object
public ClassLoader getContextClassLoader() throws SecurityException
SecurityException
- if a security manager is installed and
a call to checkPermission with a new
RuntimePermission("getClassLoader") as argument results in a
SecurityException.public void setContextClassLoader(ClassLoader cl)
cl
- the new context class loader. may be null to refer to
the application class loader.
SecurityException
- if a security manager is installed and
a call to checkPermission with a new
RuntimePermission("setContextClassLoader") as argument results in a
SecurityException.public static boolean holdsLock(Object obj)
obj
- the object we want to check whether we hold a lock on.
NullPointerExceptions
- if obj is null.public StackTraceElement[] getStackTrace()
public static Map<Thread,StackTraceElement[]> getAllStackTraces()
public long getId()
public Thread.State getState()
public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)
eh
- the new uncaught exception handler, null to reset to
default handler.public static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()
public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()
public void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)
eh
- the new uncaught exception handler, null to reset the
uncaught exception handler to the default handler (the thread
group).
SecurityException
- if a security manager is installed and
a call to checkAccess(this) causes a SecurityException.
|
![]() |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |