java.lang.Object
|
+--java.lang.Thread
A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Every thread has a priority. Threads with higher priority are
executed in preference to threads with lower priority. Each thread
may or may not also be marked as a daemon. When code running in
some thread creates a new Thread object, the new
thread has its priority initially set equal to the priority of the
creating thread, and is a daemon thread if and only if the
creating thread is a daemon.
When a Java Virtual Machine starts up, there is usually a single
non-daemon thread (which typically calls the method named
main of some designated class). The Java Virtual
Machine continues to execute threads until either of the following
occurs:
exit method of class Runtime has been
called and the security manager has permitted the exit operation
to take place.
run method or by
throwing an exception that propagates beyond the run
method.
There are two ways to create a new thread of execution. One is to
declare a class to be a subclass of Thread. This
subclass should override the run method of class
Thread. An instance of the subclass can then be
allocated and started. For example, a thread that computes primes
larger than a stated value could be written as follows:
class PrimeThread extends Thread {
long minPrime;
PrimeThread(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime
. . .
}
}
The following code would then create a thread and start it running:
PrimeThread p = new PrimeThread(143);
p.start();
The other way to create a thread is to declare a class that
implements the Runnable interface. That class then
implements the run method. An instance of the class can
then be allocated, passed as an argument when creating
Thread, and started. The same example in this other
style looks like the following:
class PrimeRun implements Runnable {
long minPrime;
PrimeRun(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime
. . .
}
}
The following code would then create a thread and start it running:
PrimeRun p = new PrimeRun(143);
new Thread(p).start();
Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it.
Runnable,
Runtime.exit(int),
run(),
stop()
| Field Summary | |
static int |
MAX_PRIORITY
copy-> MAX_PRIORITY
|
static int |
MIN_PRIORITY
copy-> MIN_PRIORITY
|
static int |
NORM_PRIORITY
copy-> NORM_PRIORITY
|
| Constructor Summary | |
Thread
copy-> new Thread()copy-> <Thread var> = new Thread();
|
|
Thread
copy-> new Thread( )copy-> <Thread var> = new Thread(<Runnable target>);
|
|
Thread
copy-> new Thread(, )copy-> <Thread var> = new Thread(<Runnable target>, <String name>);
|
|
Thread
copy-> new Thread( )copy-> <Thread var> = new Thread(<String name>);
|
|
Thread
copy-> new Thread(, )copy-> <Thread var> = new Thread(<ThreadGroup group>, <Runnable target>);
|
|
Thread
copy-> new Thread(, , )copy-> <Thread var> = new Thread(<ThreadGroup group>, <Runnable target>, <String name>);
|
|
Thread
copy-> new Thread(, )copy-> <Thread var> = new Thread(<ThreadGroup group>, <String name>);
|
|
| Method Summary | |
static int |
activeCount()
copy-> Thread.activeCount()copy-> <int var>=Thread.activeCount();
|
void |
checkAccess()
copy-> .checkAccess()copy-> <Thread>.checkAccess();
|
int |
countStackFrames()
copy-> .countStackFrames()copy-> <int var>=<Thread>.countStackFrames();
|
static Thread |
currentThread()
copy-> Thread.currentThread()copy-> <Thread var>=Thread.currentThread();
|
void |
destroy()
copy-> .destroy()copy-> <Thread>.destroy();
|
static void |
dumpStack()
copy-> Thread.dumpStack()copy-> Thread.dumpStack();
|
static int |
enumerate(Thread[] tarray)
copy-> Thread.enumerate( )copy-> <int var>=Thread.enumerate(<Thread[] tarray>);
|
ClassLoader |
getContextClassLoader()
copy-> .getContextClassLoader()copy-> <ClassLoader var>=<Thread>.getContextClassLoader();
|
String |
getName()
copy-> .getName()copy-> <String var>=<Thread>.getName();
|
int |
getPriority()
copy-> .getPriority()copy-> <int var>=<Thread>.getPriority();
|
ThreadGroup |
getThreadGroup()
copy-> .getThreadGroup()copy-> <ThreadGroup var>=<Thread>.getThreadGroup();
|
void |
interrupt()
copy-> .interrupt()copy-> <Thread>.interrupt();
|
static boolean |
interrupted()
copy-> Thread.interrupted()copy-> <boolean var>=Thread.interrupted();
|
boolean |
isAlive()
copy-> .isAlive()copy-> <boolean var>=<Thread>.isAlive();
|
boolean |
isDaemon()
copy-> .isDaemon()copy-> <boolean var>=<Thread>.isDaemon();
|
boolean |
isInterrupted()
copy-> .isInterrupted()copy-> <boolean var>=<Thread>.isInterrupted();
|
void |
join()
copy-> .join()copy-> <Thread>.join();
|
void |
join(long millis)
copy-> .join( )copy-> <Thread>.join(<long millis>);
|
void |
join(long millis,
int nanos)
copy-> .join(, )copy-> <Thread>.join(<long millis>, <int nanos>);
|
void |
resume()
copy-> .resume()copy-> <Thread>.resume();
|
void |
run()
copy-> .run()copy-> <Thread>.run();
|
void |
setContextClassLoader(ClassLoader cl)
copy-> .setContextClassLoader( )copy-> <Thread>.setContextClassLoader(<ClassLoader cl>);
|
void |
setDaemon(boolean on)
copy-> .setDaemon( )copy-> <Thread>.setDaemon(<boolean on>);
|
void |
setName(String name)
copy-> .setName( )copy-> <Thread>.setName(<String name>);
|
void |
setPriority(int newPriority)
copy-> .setPriority( )copy-> <Thread>.setPriority(<int newPriority>);
|
static void |
sleep(long millis)
copy-> Thread.sleep( )copy-> Thread.sleep(<long millis>);
|
static void |
sleep(long millis,
int nanos)
copy-> Thread.sleep(, )copy-> Thread.sleep(<long millis>, <int nanos>);
|
void |
start()
copy-> .start()copy-> <Thread>.start();
|
void |
stop()
copy-> .stop()copy-> <Thread>.stop();
|
void |
stop(Throwable obj)
copy-> .stop( )copy-> <Thread>.stop(<Throwable obj>);
|
void |
suspend()
copy-> .suspend()copy-> <Thread>.suspend();
|
String |
toString()
copy-> .toString()copy-> <String var>=<Thread>.toString(); |
static void |
yield()
copy-> Thread.yield()copy-> Thread.yield();
|
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |