📄 javalang.doc18.html
字号:
<html>
<head>
<title>The Java Language Specification The Package java.lang </title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="javalang.doc17.html">Prev</a> | <a href="javalang.doc19.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="2658"></a>
<center><h1>20.20 The Class <code>java.lang.Thread</code></h1></center>
<a name="7454"></a>
A thread is a single sequential flow of control. Thread objects allow multithreaded
Java programming; a single Java Virtual Machine can execute many threads in an
interleaved or concurrent manner.
<p><a name="25915"></a>
In the method descriptions that follow, it is very important to distinguish among "the current thread" (the thread executing the method), "this <code>Thread</code>" (the object for which the method was invoked), and "this thread" (the thread that is represented by the <code>Thread</code> object for which the method was invoked).<p>
<pre><a name="14147"></a>
<a name="7452"></a>public class <code><b>Thread</b></code> implements Runnable {
<a name="2714"></a> public final static int <code><b>MIN_PRIORITY</b></code> = 1;
<a name="2715"></a> public final static int <code><b>MAX_PRIORITY</b></code> = 10;
<a name="2716"></a> public final static int <code><b>NORM_PRIORITY</b></code> = 5;
<a name="2717"></a> public <code><b>Thread</b></code>();
<a name="7758"></a> public <code><b>Thread</b></code>(String name);
<a name="2718"></a> public <code><b>Thread</b></code>(Runnable runObject);
<a name="2720"></a> public <code><b>Thread</b></code>(Runnable runObject, String name);
<a name="7764"></a> public <code><b>Thread</b></code>(ThreadGroup group, String name)
<a name="7869"></a> throws SecurityException, <code>IllegalThreadStateException</code>;
<a name="7871"></a> public <code><b>Thread</b></code>(ThreadGroup group, Runnable runObject)
<a name="2721"></a> throws SecurityException, <code>IllegalThreadStateException</code>;
<a name="7876"></a> public <code><b>Thread</b></code>(ThreadGroup group, Runnable runObject,
<a name="21736"></a> String name)
<a name="27379"></a> throws SecurityException, <code>IllegalThreadStateException</code>;
<a name="2724"></a> public String <code><b>toString</b></code>();
<a name="8067"></a> public void <code><b>checkAccess</b></code>() throws SecurityException;
<a name="2725"></a> public void <code><b>run</b></code>();
<a name="2726"></a> public void <code><b>start</b></code>()
<a name="8116"></a> throws IllegalThreadStateException;
<a name="2727"></a> public final void <code><b>stop</b></code>()
<a name="8377"></a> throws SecurityException;
<a name="8379"></a> public final void <code><b>stop</b></code>(Throwable thr)
<a name="2728"></a> throws SecurityException, NullPointerException;
<a name="8384"></a> public final void <code><b>suspend</b></code>()
<a name="8080"></a> throws SecurityException;
<a name="8389"></a> public final void <code><b>resume</b></code>()
<a name="8081"></a> throws SecurityException;
<a name="8059"></a> public final String <code><b>getName</b></code>();
<a name="8368"></a> public final void <code><b>setName</b></code>(String name)
<a name="8060"></a> throws SecurityException;
<a name="8061"></a> public final ThreadGroup <code><b>getThreadGroup</b></code>();
<a name="8034"></a> public final int <code><b>getPriority</b></code>();
<a name="2736"></a> public final void <code><b>setPriority</b></code>(int newPriority)
<a name="8366"></a> throws SecurityException, IllegalArgumentException;
<a name="8052"></a> public final boolean <code><b>isDaemon</b></code>();
<a name="8373"></a> public final void <code><b>setDaemon</b></code>(boolean on)
<a name="52077"></a> throws SecurityException, IllegalThreadStateException;
<a name="52081"></a> public final boolean <code><b>isAlive</b></code>();
<a name="52078"></a> public int <code><b>countStackFrames</b></code>();
<a name="52079"></a> public final void <code><b>join</b></code>()
<a name="2780"></a> throws InterruptedException;
<a name="2744"></a> public final void <code><b>join</b></code>(long millis)
<a name="2745"></a> throws InterruptedException;
<a name="2746"></a> public final void <code><b>join</b></code>(long millis, int nanos)
<a name="2747"></a> throws InterruptedException;
<a name="29186"></a> public void <code><b>interrupt</b></code>();
<a name="29187"></a> public boolean <code><b>isInterrupted</b></code>();
<a name="29188"></a> public static boolean <code><b>interrupted</b></code>();
<a name="26549"></a> public static Thread <code><b>currentThread</b></code>();
<a name="8073"></a> public static int <code><b>activeCount</b></code>(); // deprecated
<a name="8074"></a> public static int <code><b>enumerate</b></code>(Thread tarray[]); // deprecated
<a name="2750"></a> public static void <code><b>dumpStack</b></code>();
<a name="2755"></a> public static void <code><b>yield</b></code>();
<a name="2756"></a> public static void <code><b>sleep</b></code>(long millis)<br>
throws InterruptedException;
<a name="2757"></a> public static void <code><b>sleep</b></code>(long millis, int nanos)<br>
throws InterruptedException;
<a name="52185"></a> public void <code><b>destroy</b></code>();
<a name="7681"></a>}
</pre><a name="7636"></a>
When a new <code>Thread</code> object is created, the thread it represents is not yet active. It is activated when some other thread calls the <code>start</code> method <a href="javalang.doc18.html#8093">(§20.20.14)</a> of the <code>Thread</code> object. This causes the thread represented by the <code>Thread</code> object to invoke the <code>run</code> method <a href="javalang.doc18.html#2686">(§20.20.13)</a> of the <code>Thread</code> object. The newly activated thread then remains alive until it stops because one of five things occurs:<p>
<ul><a name="26078"></a>
<li>The initial invocation of the <code>run</code> method by the newly activated thread completes normally through a normal return from the <code>run</code> method.
<a name="26082"></a>
<li>The initial invocation of the <code>run</code> method by the newly activated thread completes abruptly because an exception was thrown.
<a name="26061"></a>
<li>The thread invokes the <code>stop</code> method <a href="javalang.doc18.html#8095">(§20.20.15)</a> of the <code>Thread</code> object (and the security manager <a href="javalang.doc16.html#14109">(§20.17.11)</a> approves execution of the <code>stop</code> operation).
<a name="25977"></a>
<li>Some other thread invokes the <code>stop</code> method of the <code>Thread</code> object (and the security  manager <a href="javalang.doc16.html#14109">(§20.17.11)</a> approves execution of the <code>stop</code> operation).
<a name="25971"></a>
<li>Some thread invokes the <code>exit</code> method <a href="javalang.doc15.html#34351">(§20.16.2)</a> of class <code>Runtime</code> (and the security manager <a href="javalang.doc16.html#14111">(§20.17.13)</a> approves execution of the <code>exit</code> operation); this stops every thread being run by the Java Virtual Machine that is running the thread that invokes the <code>exit</code> method.
</ul><a name="25931"></a>
As a thread dies, the <code>notifyAll</code> method <a href="javalang.doc1.html#13790">(§20.1.10)</a> is invoked for the <code>Thread</code> object that represents it; this fact is important for the proper operation of the <code>join</code> methods (<a href="javalang.doc18.html#2773">§20.20.28</a>, <a href="javalang.doc18.html#14183">§20.20.29</a>, <a href="javalang.doc18.html#14185">§20.20.30</a>). A thread is also removed from its thread group as it dies. Once a thread has been stopped, it is no longer alive and it cannot be restarted.<p>
<a name="26148"></a>
A thread that is alive can be <i>suspended</i> and <i>resumed</i>. A suspended thread is considered to be alive, but it performs no work, makes no progress, executes no virtual machine instructions. Resumption restores a thread to the state of active execution. A thread is suspended when it or another thread calls the <code>suspend</code> method <a href="javalang.doc18.html#8097">(§20.20.17)</a> of the <code>Thread</code> object that represents it (and the security manager <a href="javalang.doc16.html#14109">(§20.17.11)</a> approves execution of the <code>suspend</code> operation). A thread is resumed when another thread calls the <code>resume</code> method <a href="javalang.doc18.html#8098">(§20.20.18)</a> of the <code>Thread</code> object that represents it (and the security manager <a href="javalang.doc16.html#14109">(§20.17.11)</a> approves execution of the <code>resume</code> operation).<p>
<a name="26144"></a>
Every thread has a <i>priority</i>. When there is competition for processing resources, threads with higher priority are generally executed in preference to threads with lower priority. Such preference is not, however, a guarantee that the highest priority thread will always be running, and thread priorities cannot be used to implement mutual exclusion. When code running in some thread creates a new <code>Thread</code> object, the newly created thread has its priority initially set equal to the priority of the creating thread. But the priority of a thread <i>T</i> may be changed at any time if some thread invokes the <code>setPriority</code> method of the <code>Thread</code> object that represents <i>T</i> (and the security manager <a href="javalang.doc16.html#14109">(§20.17.11)</a> approves execution of the <code>setPriority</code> operation).<p>
<a name="26187"></a>
Each thread may or may not be marked as a <i>daemon</i>. When code running in some thread creates a new <code>Thread</code> object, the newly created thread is a daemon thread if and only if the creating thread is a daemon. But the daemonhood of a thread <i>T</i> may be changed before it is activated if some other thread invokes the <code>setDaemon</code> method of the <code>Thread</code> object that represents <i>T</i> (and the security manager <a href="javalang.doc16.html#14109">(§20.17.11)</a> approves execution of the <code>setDaemon</code> operation).<p>
<a name="52128"></a>
<p>
<a name="52129"></a>
<p>
<a name="52130"></a>
<p>
<a name="52131"></a>
<p>
<a name="52132"></a>
<p>
<a name="7637"></a>
When a Java Virtual Machine starts up, there is usually a single non-daemon thread, which typically begins by invoking the method <code>main</code> of some designated class. The Java Virtual Machine continues to execute threads according to the thread execution model until all threads that are not daemon threads have stopped.<p>
<a name="7649"></a>
There are two ways to create a new thread of execution. One is to declare some class to be a subclass of <code>Thread</code>; this subclass should override the <code>run</code> method of class <code>Thread</code>. An instance of the subclass can then be created and started. For example, consider code for a thread whose job is to compute primes larger than a stated value:<p>
<pre><a name="7650"></a>
class PrimeThread extends Thread {
<br><a name="7651"></a> long minPrime;
<br><a name="7652"></a> PrimeThread(long minPrime) {
<a name="7653"></a> this.minPrime = minPrime;
<a name="7654"></a> }
<br><a name="7655"></a> public void run() {
<a name="7656"></a> // compute primes larger than minPrime
<a name="7657"></a> ...
<a name="7658"></a> }
<br><a name="7659"></a>}
</pre><a name="7660"></a>
The following code would then create a thread and start it running:<p>
<pre><a name="7661"></a>
PrimeThread p = new PrimeThread(143);
<a name="7662"></a>p.start();
</pre><a name="7663"></a>
The other way to create a thread is to is to declare some class to implement the <code>Runnable</code> interface, which also requires that the class implement the <code>run</code> method. An instance of the class can then be created, used to create a <code>Thread</code>, and started. The same example in this other style looks like this:<p>
<pre><a name="7664"></a>
class PrimeRun implements Runnable {
<br><a name="7665"></a> long minPrime;
<br><a name="7666"></a> PrimeRun(long minPrime) {
<a name="7667"></a> this.minPrime = minPrime;
<a name="7668"></a> }
<br><a name="7669"></a>
public void run() {
<a name="7670"></a> // compute primes larger than minPrime
<a name="7671"></a> ...
<a name="7672"></a> }
<br><a name="7673"></a>}
</pre><a name="34753"></a>
The following code would then create a thread and start it running:<p>
<pre><a name="7675"></a>
PrimeRun p = new PrimeRun(143);
<a name="7676"></a>new Thread(p).start();
</pre><a name="7708"></a>
Every thread has a name, which is a <code>String</code>, 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.<p>
<a name="26328"></a>
Every thread that has not yet been stopped belongs to a thread group <a href="javalang.doc19.html#14469">(§20.21)</a>. A thread can always create a new thread in its own thread group. To create a thread in some other thread group requires the approval of the <code>checkAccess</code> method <a href="javalang.doc19.html#27041">(§20.21.4)</a> of that thread group, which forwards the decision to the security manager <a href="javalang.doc16.html#14109">(§20.17.11)</a>.<p>
<a name="14148"></a>
<p><font size=+1><strong>20.20.1 </strong> <code>public final static int <code><b>MIN_PRIORITY</b></code> = 1;</code></font>
<p>
<a name="7683"></a>
The constant value of this field is <code>1</code>, the smallest allowed priority for a thread.
<p><a name="2662"></a>
<p><font size=+1><strong>20.20.2 </strong> <code>public final static int <code><b>MAX_PRIORITY</b></code> = 10;</code></font>
<p>
<a name="7691"></a>
The constant value of this field is <code>10</code>, the largest allowed priority value for a
thread.
<p><a name="14149"></a>
<p><font size=+1><strong>20.20.3 </strong> <code>public final static int <code><b>NORM_PRIORITY</b></code> = 5;</code></font>
<p>
<a name="7695"></a>
The constant value of this field is <code>5</code>, the normal priority for a thread that is not a
daemon.
<p><a name="14157"></a>
<p><font size=+1><strong>20.20.4 </strong> <code>public <code><b>Thread</b></code>()</code></font>
<p>
<a name="7705"></a>
This constructor initializes a newly created <code>Thread</code> object so that it has no separate
run object, has a newly generated name, and belongs to the same thread group
as the thread that is creating the new thread.
<p><a name="7717"></a>
This constructor has exactly the same effect as the explicit constructor call <code>this(null, null, </code><i>gname</i><code>)</code> <a href="javalang.doc18.html#14163">(§20.20.10)</a>, where <i>gname</i> is a newly generated name. Automatically generated names are of the form <code>"Thread-"+</code><i>n</i>, where <i>n</i> is an integer.<p>
<a name="7752"></a>
<p><font size=+1><strong>20.20.5 </strong> <code>public <code><b>Thread</b></code>(String name)</code></font>
<p>
<a name="7768"></a>
This constructor initializes a newly created <code>Thread</code> object so that it has no separate
run object, has the specified <code>name</code> as its name, and belongs to the same thread
group as the thread that is creating the new thread.
<p><a name="7772"></a>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -