|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.lang.Thread
A Thread is a single sequential flow of control within a process. This simply means that while executing within a program, each thread has a beginning, a sequence, a point of execution occurring at any time during runtime of the thread and of course, an ending. Thread objects are the basis for multi-threaded programming. Multi-threaded programming allows a single program to conduct concurrently running threads that perform different tasks.
To create a new thread of execution, declare a new class which is a subclass of Thread and then override the run() method with code that you want executed in this Thread. An instance of the Thread subclass should be created next with a call to the start() method following the instance. The start() method will create the thread and execute the run() method. For example:
class PrimeThread extends Thread { public void run() { // compute primes... } }To start this thread you need to do the following:
PrimeThread p = new PrimeThread(); p.start(); ...Another way to create a thread is by using the Runnable interface. This way any object that implements the Runnable interface can be run in a thread. For example:
class Primes implements Runnable { public void run() { // compute primes... } }To start this thread you need to do the following:
Primes p = new Primes(); new Thread(p).start(); ...The virtual machine runs until all Threads that are not daemon Threads have died. A Thread dies when its run() method returns, or when the stop() method is called.
When a new Thread is created, it inherits the priority and the daemon flag from its parent (i.e.: the Thread that created it).
Runnable
Field Summary | |
static int |
MAX_PRIORITY
The maximum priority that a Thread can have. |
static int |
MIN_PRIORITY
The minimum priority that a Thread can have. |
static int |
NORM_PRIORITY
The default priority that is assigned to a Thread. |
Constructor Summary | |
Thread()
Constructs a new Thread. |
|
Thread(Runnable target)
Constructs a new Thread which applies the run() method of the specified target. |
|
Thread(Runnable target,
String name)
Constructs a new Thread with the specified name and applies the run() method of the specified target. |
|
Thread(String name)
Constructs a new Thread with the specified name. |
|
Thread(ThreadGroup group,
Runnable target)
Constructs a new Thread in the specified Thread group that applies the run() method of the specified target. |
|
Thread(ThreadGroup group,
Runnable target,
String name)
Constructs a new Thread in the specified Thread group with the specified name and applies the run() method of the specified target. |
|
Thread(ThreadGroup group,
String name)
Constructs a new Thread in the specified Thread group with the specified name. |
Method Summary | |
static int |
activeCount()
Returns the current number of active Threads in this Thread group. |
void |
checkAccess()
Checks whether the current Thread is allowed to modify this Thread. |
int |
countStackFrames()
Returns the number of stack frames in this Thread. |
static Thread |
currentThread()
Returns a reference to the currently executing Thread object. |
void |
destroy()
Destroy a thread, without any cleanup, i.e. |
static void |
dumpStack()
A debugging procedure to print a stack trace for the current Thread. |
static int |
enumerate(Thread[] tarray)
Copies, into the specified array, references to every active Thread in this Thread's group. |
String |
getName()
Gets and returns this Thread's name. |
int |
getPriority()
Gets and returns the Thread's priority. |
ThreadGroup |
getThreadGroup()
Gets and returns this Thread group. |
void |
interrupt()
Send an interrupt to a thread. |
static boolean |
interrupted()
Ask if you have been interrupted. |
boolean |
isAlive()
Returns a boolean indicating if the Thread is active. |
boolean |
isDaemon()
Returns the daemon flag of the Thread. |
boolean |
isInterrupted()
Ask if another thread has been interrupted. |
void |
join()
Waits forever for this Thread to die. |
void |
join(long millis)
Waits for this Thread to die. |
void |
join(long millis,
int nanos)
Waits for the Thread to die, with more precise time. |
void |
resume()
Resumes this Thread execution. |
void |
run()
The actual body of this Thread. |
void |
setDaemon(boolean on)
Marks this Thread as a daemon Thread or a user Thread. |
void |
setName(String name)
Sets the Thread's name. |
void |
setPriority(int newPriority)
Sets the Thread's priority. |
static void |
sleep(long millis)
Causes the currently executing Thread to sleep for the specified number of milliseconds. |
static void |
sleep(long millis,
int nanos)
Sleep, in milliseconds and additional nanosecond. |
void |
start()
Starts this Thread. |
void |
stop()
Stops a Thread by tossing an object. |
void |
stop(Throwable o)
Stops a Thread by tossing an object. |
void |
suspend()
Suspends this Thread's execution. |
String |
toString()
Returns a String representation of the Thread, including the thread's name, priority and thread group. |
static void |
yield()
Causes the currently executing Thread object to yield. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final int MIN_PRIORITY
public static final int NORM_PRIORITY
public static final int MAX_PRIORITY
Constructor Detail |
public Thread()
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++; }
public Thread(Runnable target)
target
- the object whose run() method is calledpublic Thread(ThreadGroup group, Runnable target)
group
- the Thread grouptarget
- the object whose run() method is calledpublic Thread(String name)
name
- the name of the new Threadpublic Thread(ThreadGroup group, String name)
group
- the Thread groupname
- the name of the new Threadpublic Thread(Runnable target, String name)
target
- the object whose run() method is calledname
- the name of the new Threadpublic Thread(ThreadGroup group, Runnable target, String name)
group
- the Thread grouptarget
- the object whose run() method is calledname
- the name of the new ThreadMethod Detail |
public static Thread currentThread()
public static void yield()
public static void sleep(long millis) throws InterruptedException
millis
- the length of time to sleep in milliseconds
InterruptedException
- Another thread has interrupted this thread.public static void sleep(long millis, int nanos) throws InterruptedException
millis
- the length of time to sleep in millisecondsnanos
- 0-999999 additional nanoseconds to sleep
InterruptedException
- Another thread has interrupted this thread.public void start()
IllegalThreadStateException
- If the thread was already started.run()
,
stop()
public void run()
run
in interface Runnable
start()
,
stop()
public final void stop()
start()
,
run()
public final void stop(Throwable o)
o
- the Throwable object to be thrownstart()
,
run()
public void interrupt()
public static boolean interrupted()
public boolean isInterrupted()
public void destroy()
public final boolean isAlive()
public final void suspend()
public final void resume()
public final void setPriority(int newPriority)
IllegalArgumentException
- If the priority is not within the
range MIN_PRIORITY, MAX_PRIORITY.MIN_PRIORITY
,
MAX_PRIORITY
,
getPriority()
public final int getPriority()
setPriority(int)
public final void setName(String name)
name
- the new name of the ThreadgetName()
public final String getName()
setName(java.lang.String)
public final ThreadGroup getThreadGroup()
public static int activeCount()
public static int enumerate(Thread[] tarray)
public int countStackFrames()
IllegalThreadStateException
- If the Thread is not suspended.public final void join(long millis) throws InterruptedException
millis
- the time to wait in milliseconds
InterruptedException
- Another thread has interrupted this thread.public final void join(long millis, int nanos) throws InterruptedException
InterruptedException
- Another thread has interrupted this thread.public final void join() throws InterruptedException
InterruptedException
- Another thread has interrupted this thread.public static void dumpStack()
Throwable.printStackTrace()
public final void setDaemon(boolean on)
on
- determines whether the Thread will be a daemon Thread
IllegalThreadStateException
- If the Thread is active.isDaemon()
public final boolean isDaemon()
setDaemon(boolean)
public void checkAccess()
SecurityException
- If the current Thread is not allowed
to access this Thread group.public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |