Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
java.lang.Thread
run()
method, at which point you may start the
Thread by calling its start()
method, or you may implement
Runnable
in the class you want to use and then call new
Thread(your_obj).start()
.
The virtual machine runs until all non-daemon threads have died (either
by returning from the run() method as invoked by start(), or by throwing
an uncaught exception); or until System.exit
is called with
adequate permissions.
It is unclear at what point a Thread should be added to a ThreadGroup,
and at what point it should be removed. Should it be inserted when it
starts, or when it is created? Should it be removed when it is suspended
or interrupted? The only thing that is clear is that the Thread should be
removed when it is stopped.
Runnable
, Runtime.exit(int)
, run()
, start()
, ThreadLocal
Field Summary | |
static int |
|
static int |
|
static int |
|
Constructor Summary | |
| |
| |
| |
| |
|
Method Summary | |
static int |
|
void |
|
int |
|
static Thread |
|
void |
|
static void |
|
static int | |
ClassLoader |
|
String |
|
int |
|
ThreadGroup |
|
static boolean | |
void |
|
static boolean |
|
boolean |
|
boolean |
|
boolean |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void |
|
void | |
void |
|
static void |
|
static void |
|
void |
|
void |
|
void | |
void |
|
String |
|
static void |
|
Methods inherited from class java.lang.Object | |
clone , equals , finalize , getClass , hashCode , notify , notifyAll , toString , wait , wait , wait |
public static final int NORM_PRIORITY
The priority a Thread gets by default.
- Field Value:
- 5
public Thread()
Allocates a newThread
object. This constructor has the same effect asThread(null, null,
gname)
, where gname is a newly generated name. Automatically generated names are of the form"Thread-"+
n, where n is an integer. Threads created this way must have overridden theirrun()
method to actually do anything. An example illustrating this method being used follows:import java.lang.*; class plain01 implements Runnable { String name; plain01() { name = null; } plain01(String s) { name = s; } public void run() { if (name == null) System.out.println("A new thread created"); else System.out.println("A new thread with name " + name + " created"); } } class threadtest01 { public static void main(String args[] ) { int failed = 0 ; Thread t1 = new Thread(); if (t1 != null) System.out.println("new Thread() succeed"); else { System.out.println("new Thread() failed"); failed++; } } }
- See Also:
Thread(ThreadGroup,Runnable,String)
public Thread(Runnable target)
Allocates a newThread
object. This constructor has the same effect asThread(null, target,
gname)
, where gname is a newly generated name. Automatically generated names are of the form"Thread-"+
n, where n is an integer.
- Parameters:
target
- the object whoserun
method is called.
- See Also:
Thread(ThreadGroup,Runnable,String)
public Thread(Runnable target, String name)
Allocates a newThread
object. This constructor has the same effect asThread(null, target, name)
.
- Parameters:
target
- the Runnable object to executename
- the name for the Thread
- Throws:
NullPointerException
- if name is null
- See Also:
Thread(ThreadGroup,Runnable,String)
public Thread(String name)
Allocates a newThread
object. This constructor has the same effect asThread(null, null, name)
.
- Parameters:
name
- the name of the new thread.
- See Also:
Thread(ThreadGroup,Runnable,String)
public Thread(ThreadGroup group, Runnable target)
Allocates a newThread
object. This constructor has the same effect asThread(group, target,
gname)
, where gname is a newly generated name. Automatically generated names are of the form"Thread-"+
n, where n is an integer.
- Parameters:
group
- the group to put the Thread intotarget
- the Runnable object to execute
- Throws:
SecurityException
- if this thread cannot accessgroup
IllegalThreadStateException
- if group is destroyed
- See Also:
Thread(ThreadGroup,Runnable,String)
public Thread(ThreadGroup group, Runnable target, String name)
Allocate a new Thread object, with the specified ThreadGroup and name, and using the specified Runnable object'srun()
method to execute. If the Runnable object is null,this
(which is a Runnable) is used instead. If the ThreadGroup is null, the security manager is checked. If a manager exists and returns a non-null object forgetThreadGroup
, that group is used; otherwise the group of the creating thread is used. Note that the security manager callscheckAccess
if the ThreadGroup is not null. The new Thread will inherit its creator's priority and daemon status. These can be changed withsetPriority
andsetDaemon
.
- Parameters:
group
- the group to put the Thread intotarget
- the Runnable object to executename
- the name for the Thread
- Throws:
NullPointerException
- if name is nullSecurityException
- if this thread cannot accessgroup
IllegalThreadStateException
- if group is destroyed
public Thread(ThreadGroup group, Runnable target, String name, long size)
Allocate a new Thread object, as if byThread(group, null, name)
, and give it the specified stack size, in bytes. The stack size is highly platform independent, and the virtual machine is free to round up or down, or ignore it completely. A higher value might let you go longer before aStackOverflowError
, while a lower value might let you go longer before anOutOfMemoryError
. Or, it may do absolutely nothing! So be careful, and expect to need to tune this value if your virtual machine even supports it.
- Parameters:
group
- the group to put the Thread intotarget
- the Runnable object to executename
- the name for the Threadsize
- the stack size, in bytes; 0 to be ignored
- Throws:
NullPointerException
- if name is nullSecurityException
- if this thread cannot accessgroup
IllegalThreadStateException
- if group is destroyed
- Since:
- 1.4
public Thread(ThreadGroup group, String name)
Allocates a newThread
object. This constructor has the same effect asThread(group, null, name)
- Parameters:
group
- the group to put the Thread intoname
- the name for the Thread
- Throws:
NullPointerException
- if name is nullSecurityException
- if this thread cannot accessgroup
IllegalThreadStateException
- if group is destroyed
- See Also:
Thread(ThreadGroup,Runnable,String)
public static int activeCount()
Get the number of active threads in the current Thread's ThreadGroup. This implementation callscurrentThread().getThreadGroup().activeCount()
.
- Returns:
- the number of active threads in the current ThreadGroup
- See Also:
ThreadGroup.activeCount()
public final void checkAccess()
Check whether the current Thread is allowed to modify this Thread. This passes the check on toSecurityManager.checkAccess(this)
.
- Throws:
SecurityException
- if the current Thread cannot modify this Thread
- See Also:
SecurityManager.checkAccess(Thread)
public int countStackFrames()
Deprecated. pointless, since suspend is deprecated
Count the number of stack frames in this Thread. The Thread in question must be suspended when this occurs.
- Returns:
- the number of stack frames in this Thread
- Throws:
IllegalThreadStateException
- if this Thread is not suspended
public static Thread currentThread()
Get the currently executing Thread.
- Returns:
- the currently executing Thread
public void destroy()
Originally intended to destroy this thread, this method was never implemented by Sun, and is hence a no-op.
public static void dumpStack()
Print a stack trace of the current thread to stderr using the same format as Throwable's printStackTrace() method.
- See Also:
Throwable.printStackTrace()
public static int enumerate(Thread[] array)
Copy every active thread in the current Thread's ThreadGroup into the array. Extra threads are silently ignored. This implementation callsgetThreadGroup().enumerate(array)
, which may have a security check,checkAccess(group)
.
- Parameters:
array
- the array to place the Threads into
- Returns:
- the number of Threads placed into the array
- Throws:
NullPointerException
- if array is nullSecurityException
- if you cannot access the ThreadGroup
public ClassLoader getContextClassLoader()
Returns the context classloader of this Thread. The context classloader can be used by code that want to load classes depending on the current thread. Normally classes are loaded depending on the classloader of the current class. There may be a security check forRuntimePermission("getClassLoader")
if the caller's class loader is not null or an ancestor of this thread's context class loader.
- Returns:
- the context class loader
- Throws:
SecurityException
- when permission is denied
- Since:
- 1.2
- See Also:
setContextClassLoader(ClassLoader)
public final ThreadGroup getThreadGroup()
Get the ThreadGroup this Thread belongs to. If the thread has died, this returns null.
- Returns:
- this Thread's ThreadGroup
public static boolean holdsLock(Object obj)
Checks whether the current thread holds the monitor on a given object. This allows you to doassert Thread.holdsLock(obj)
.
- Parameters:
obj
- the object to test lock ownership on.
- Returns:
- true if the current thread is currently synchronized on obj
- Throws:
NullPointerException
- if obj is null
- Since:
- 1.4
public void interrupt()
Interrupt this Thread. First, there is a security check,checkAccess
. Then, depending on the current state of the thread, various actions take place: If the thread is waiting because ofwait()
,sleep(long)
, orjoin()
, its interrupt status will be cleared, and an InterruptedException will be thrown. Notice that this case is only possible if an external thread called interrupt(). If the thread is blocked in an interruptible I/O operation, inInterruptibleChannel
, the interrupt status will be set, and ClosedByInterruptException will be thrown. If the thread is blocked on aSelector
, the interrupt status will be set, and the selection will return, with a possible non-zero value, as though by the wakeup() method. Otherwise, the interrupt status will be set.
- Throws:
SecurityException
- if you cannot modify this Thread
public static boolean interrupted()
Determine whether the current Thread has been interrupted, and clear the interrupted status in the process.
- Returns:
- whether the current Thread has been interrupted
- See Also:
isInterrupted()
public final boolean isAlive()
Determine whether this Thread is alive. A thread which is alive has started and not yet died.
- Returns:
- whether this Thread is alive
public final boolean isDaemon()
Tell whether this is a daemon Thread or not.
- Returns:
- whether this is a daemon Thread or not
- See Also:
setDaemon(boolean)
public boolean isInterrupted()
Determine whether the given Thread has been interrupted, but leave the interrupted status alone in the process.
- Returns:
- whether the Thread has been interrupted
- See Also:
interrupted()
public final void join() throws InterruptedException
Wait forever for the Thread in question to die.
- Throws:
InterruptedException
- if the Thread is interrupted; it's interrupted status will be cleared
public final void join(long ms) throws InterruptedException
Wait the specified amount of time for the Thread in question to die.
- Parameters:
ms
- the number of milliseconds to wait, or 0 for forever
- Throws:
InterruptedException
- if the Thread is interrupted; it's interrupted status will be cleared
public final void join(long ms, int ns) throws InterruptedException
Wait the specified amount of time for the Thread in question to die. Note that 1,000,000 nanoseconds == 1 millisecond, but most VMs do not offer that fine a grain of timing resolution. Besides, there is no guarantee that this thread can start up immediately when time expires, because some other thread may be active. So don't expect real-time performance.
- Parameters:
ms
- the number of milliseconds to wait, or 0 for foreverns
- the number of extra nanoseconds to sleep (0-999999)
- Throws:
InterruptedException
- if the Thread is interrupted; it's interrupted status will be clearedIllegalArgumentException
- if ns is invalid
public final void resume()
Deprecated. pointless, since suspend is deprecated
Resume a suspended thread.
- Throws:
SecurityException
- if you cannot resume the Thread
- See Also:
checkAccess()
,suspend()
public void run()
The method of Thread that will be run if there is no Runnable object associated with the Thread. Thread's implementation does nothing at all.
- See Also:
start()
,Thread(ThreadGroup,Runnable,String)
public void setContextClassLoader(ClassLoader classloader)
Sets the context classloader for this Thread. When not explicitly set, the context classloader for a thread is the same as the context classloader of the thread that created this thread. The first thread has as context classloader the system classloader. There may be a security check forRuntimePermission("setContextClassLoader")
.
- Parameters:
classloader
- the new context class loader
- Throws:
SecurityException
- when permission is denied
- Since:
- 1.2
- See Also:
getContextClassLoader()
public final void setDaemon(boolean daemon)
Set the daemon status of this Thread. If this is a daemon Thread, then the VM may exit even if it is still running. This may only be called before the Thread starts running. There may be a security check,checkAccess
.
- Parameters:
daemon
- whether this should be a daemon thread or not
- Throws:
SecurityException
- if you cannot modify this ThreadIllegalThreadStateException
- if the Thread is active
- See Also:
isDaemon()
,checkAccess()
public final void setName(String name)
Set this Thread's name. There may be a security check,checkAccess
.
- Parameters:
name
- the new name for this Thread
- Throws:
NullPointerException
- if name is nullSecurityException
- if you cannot modify this Thread
public final void setPriority(int newPriority)
Set this Thread's priority. There may be a security check,checkAccess
, then the priority is set to the smaller of priority and the ThreadGroup maximum priority.
- Parameters:
- Throws:
IllegalArgumentException
- if priority exceeds MIN_PRIORITY or MAX_PRIORITYSecurityException
- if you cannot modify this Thread
public static void sleep(long ms) throws InterruptedException
Suspend the current Thread's execution for the specified amount of time. The Thread will not lose any locks it has during this time. There are no guarantees which thread will be next to run, but most VMs will choose the highest priority thread that has been waiting longest.
- Parameters:
ms
- the number of milliseconds to sleep, or 0 for forever
- Throws:
InterruptedException
- if the Thread is interrupted; it's interrupted status will be cleared
- See Also:
notify()
,wait(long)
public static void sleep(long timeout, int nanos) throws InterruptedException
Suspend the current Thread's execution for the specified amount of time. The Thread will not lose any locks it has during this time. There are no guarantees which thread will be next to run, but most VMs will choose the highest priority thread that has been waiting longest. Note that 1,000,000 nanoseconds == 1 millisecond, but most VMs do not offer that fine a grain of timing resolution. Besides, there is no guarantee that this thread can start up immediately when time expires, because some other thread may be active. So don't expect real-time performance.
- Parameters:
- Throws:
InterruptedException
- if the Thread is interrupted; it's interrupted status will be clearedIllegalArgumentException
- if ns is invalid
- See Also:
notify()
,wait(long,int)
public void start()
Start this Thread, calling the run() method of the Runnable this Thread was created with, or else the run() method of the Thread itself. This is the only way to start a new thread; calling run by yourself will just stay in the same thread. The virtual machine will remove the thread from its thread group when the run() method completes.
- Throws:
IllegalThreadStateException
- if the thread has already started
- See Also:
run()
public final void stop()
Deprecated. unsafe operation, try not to use
Cause this Thread to stop abnormally because of the throw of a ThreadDeath error. If you stop a Thread that has not yet started, it will stop immediately when it is actually started. This is inherently unsafe, as it can interrupt synchronized blocks and leave data in bad states. Hence, there is a security check:checkAccess(this)
, plus another one if the current thread is not this:RuntimePermission("stopThread")
. If you must catch a ThreadDeath, be sure to rethrow it after you have cleaned up. ThreadDeath is the only exception which does not print a stack trace when the thread dies.
- Throws:
SecurityException
- if you cannot stop the Thread
public final void stop(Throwable t)
Deprecated. unsafe operation, try not to use
Cause this Thread to stop abnormally and throw the specified exception. If you stop a Thread that has not yet started, it will stop immediately when it is actually started. WARNINGThis bypasses Java security, and can throw a checked exception which the call stack is unprepared to handle. Do not abuse this power. This is inherently unsafe, as it can interrupt synchronized blocks and leave data in bad states. Hence, there is a security check:checkAccess(this)
, plus another one if the current thread is not this:RuntimePermission("stopThread")
. If you must catch a ThreadDeath, be sure to rethrow it after you have cleaned up. ThreadDeath is the only exception which does not print a stack trace when the thread dies.
- Parameters:
t
- the Throwable to throw when the Thread dies
- Throws:
SecurityException
- if you cannot stop the ThreadNullPointerException
- in the calling thread, if t is null
public final void suspend()
Deprecated. unsafe operation, try not to use
Suspend this Thread. It will not come back, ever, unless it is resumed. This is inherently unsafe, as the suspended thread still holds locks, and can potentially deadlock your program. Hence, there is a security check:checkAccess
.
- Throws:
SecurityException
- if you cannot suspend the Thread
- See Also:
checkAccess()
,resume()
public String toString()
Returns a string representation of this thread, including the thread's name, priority, and thread group.
- Returns:
- a human-readable String representing this Thread
public static void yield()
Causes the currently executing thread object to temporarily pause and allow other threads to execute.