- java.lang.Object
-
- java.lang.Thread
-
-
All Implemented Interfaces:
- Runnable
-
Direct Known Subclasses:
- ForkJoinWorkerThread
public class Thread extends Object implements Runnable
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
Threadobject, 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
mainof some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:- The
exitmethod of classRuntimehas been called and the security manager has permitted the exit operation to take place. - All threads that are not daemon threads have died, either by returning from the call to the
runmethod or by throwing an exception that propagates beyond therunmethod.
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 therunmethod of classThread. 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
Runnableinterface. That class then implements therunmethod. An instance of the class can then be allocated, passed as an argument when creatingThread, 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.
Unless otherwise noted, passing a
nullargument to a constructor or method in this class will cause aNullPointerExceptionto be thrown.-
Since:
- JDK1.0 See Also:
-
Runnable,Runtime.exit(int),run(),stop()
文章来源:http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html
本文详细介绍了 Java 中 Thread 类的功能及使用方法,包括如何创建和启动线程,线程的优先级和守护进程特性,以及通过继承 Thread 类或实现 Runnable 接口来创建线程的两种方式。
156

被折叠的 条评论
为什么被折叠?



