📄 thread.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd"><!--NewPage--><HTML><HEAD><!-- Generated by javadoc on Thu Apr 27 23:35:13 PDT 2000 --><TITLE>Java 2 Platform SE v1.3: Class Thread</TITLE><LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style"></HEAD><BODY BGCOLOR="white"><!-- ========== START OF NAVBAR ========== --><A NAME="navbar_top"><!-- --></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0"><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3"> <TR ALIGN="center" VALIGN="top"> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A> </TD> <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="class-use/Thread.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD> </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM><b>Java<sup><font size=-2>TM</font></sup> 2 Platform<br>Std. Ed. v1.3</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../java/lang/System.html"><B>PREV CLASS</B></A> <A HREF="../../java/lang/ThreadGroup.html"><B>NEXT CLASS</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../index.html" TARGET="_top"><B>FRAMES</B></A> <A HREF="Thread.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2"> SUMMARY: INNER | <A HREF="#field_summary">FIELD</A> | <A HREF="#constructor_summary">CONSTR</A> | <A HREF="#method_summary">METHOD</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">DETAIL: <A HREF="#field_detail">FIELD</A> | <A HREF="#constructor_detail">CONSTR</A> | <A HREF="#method_detail">METHOD</A></FONT></TD></TR></TABLE><!-- =========== END OF NAVBAR =========== --><HR><!-- ======== START OF CLASS DATA ======== --><H2><FONT SIZE="-1">java.lang</FONT><BR>Class Thread</H2><PRE><A HREF="../../java/lang/Object.html">java.lang.Object</A> | +--<B>java.lang.Thread</B></PRE><DL><DT><B>All Implemented Interfaces:</B> <DD><A HREF="../../java/lang/Runnable.html">Runnable</A></DD></DL><HR><DL><DT>public class <B>Thread</B><DT>extends <A HREF="../../java/lang/Object.html">Object</A><DT>implements <A HREF="../../java/lang/Runnable.html">Runnable</A></DL><P>A <i>thread</i> is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. <p> 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 <code>Thread</code> 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. <p> When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named <code>main</code> of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs: <ul> <li>The <code>exit</code> method of class <code>Runtime</code> has been called and the security manager has permitted the exit operation to take place. <li>All threads that are not daemon threads have died, either by returning from the call to the <code>run</code> method or by throwing an exception that propagates beyond the <code>run</code> method. </ul> <p> There are two ways to create a new thread of execution. One is to declare a 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 allocated and started. For example, a thread that computes primes larger than a stated value could be written as follows: <p><hr><blockquote><pre> class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } } </pre></blockquote><hr> <p> The following code would then create a thread and start it running: <p><blockquote><pre> PrimeThread p = new PrimeThread(143); p.start(); </pre></blockquote> <p> The other way to create a thread is to declare a class that implements the <code>Runnable</code> interface. That class then implements the <code>run</code> method. An instance of the class can then be allocated, passed as an argument when creating <code>Thread</code>, and started. The same example in this other style looks like the following: <p><hr><blockquote><pre> class PrimeRun implements Runnable { long minPrime; PrimeRun(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime . . . } } </pre></blockquote><hr> <p> The following code would then create a thread and start it running: <p><blockquote><pre> PrimeRun p = new PrimeRun(143); new Thread(p).start(); </pre></blockquote> <p> 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.<P><DL><DT><B>Since: </B><DD>JDK1.0</DD><DT><B>See Also: </B><DD><A HREF="../../java/lang/Runnable.html"><CODE>Runnable</CODE></A>, <A HREF="../../java/lang/Runtime.html#exit(int)"><CODE>Runtime.exit(int)</CODE></A>, <A HREF="../../java/lang/Thread.html#run()"><CODE>run()</CODE></A>, <A HREF="../../java/lang/Thread.html#stop()"><CODE>stop()</CODE></A></DL><HR><P><!-- ======== INNER CLASS SUMMARY ======== --><!-- =========== FIELD SUMMARY =========== --><A NAME="field_summary"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TD COLSPAN=2><FONT SIZE="+2"><B>Field Summary</B></FONT></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>static int</CODE></FONT></TD><TD><CODE><B><A HREF="../../java/lang/Thread.html#MAX_PRIORITY">MAX_PRIORITY</A></B></CODE><BR> The maximum priority that a thread can have.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>static int</CODE></FONT></TD><TD><CODE><B><A HREF="../../java/lang/Thread.html#MIN_PRIORITY">MIN_PRIORITY</A></B></CODE><BR> The minimum priority that a thread can have.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>static int</CODE></FONT></TD><TD><CODE><B><A HREF="../../java/lang/Thread.html#NORM_PRIORITY">NORM_PRIORITY</A></B></CODE><BR> The default priority that is assigned to a thread.</TD></TR></TABLE> <!-- ======== CONSTRUCTOR SUMMARY ======== --><A NAME="constructor_summary"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TD COLSPAN=2><FONT SIZE="+2"><B>Constructor Summary</B></FONT></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="../../java/lang/Thread.html#Thread()">Thread</A></B>()</CODE><BR> Allocates a new <code>Thread</code> object.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="../../java/lang/Thread.html#Thread(java.lang.Runnable)">Thread</A></B>(<A HREF="../../java/lang/Runnable.html">Runnable</A> target)</CODE><BR> Allocates a new <code>Thread</code> object.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="../../java/lang/Thread.html#Thread(java.lang.Runnable, java.lang.String)">Thread</A></B>(<A HREF="../../java/lang/Runnable.html">Runnable</A> target, <A HREF="../../java/lang/String.html">String</A> name)</CODE><BR> Allocates a new <code>Thread</code> object.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="../../java/lang/Thread.html#Thread(java.lang.String)">Thread</A></B>(<A HREF="../../java/lang/String.html">String</A> name)</CODE><BR> Allocates a new <code>Thread</code> object.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="../../java/lang/Thread.html#Thread(java.lang.ThreadGroup, java.lang.Runnable)">Thread</A></B>(<A HREF="../../java/lang/ThreadGroup.html">ThreadGroup</A> group, <A HREF="../../java/lang/Runnable.html">Runnable</A> target)</CODE><BR> Allocates a new <code>Thread</code> object.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="../../java/lang/Thread.html#Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)">Thread</A></B>(<A HREF="../../java/lang/ThreadGroup.html">ThreadGroup</A> group, <A HREF="../../java/lang/Runnable.html">Runnable</A> target, <A HREF="../../java/lang/String.html">String</A> name)</CODE><BR> Allocates a new <code>Thread</code> object so that it has <code>target</code> as its run object, has the specified <code>name</code> as its name, and belongs to the thread group referred to by <code>group</code>.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="../../java/lang/Thread.html#Thread(java.lang.ThreadGroup, java.lang.String)">Thread</A></B>(<A HREF="../../java/lang/ThreadGroup.html">ThreadGroup</A> group, <A HREF="../../java/lang/String.html">String</A> name)</CODE><BR> Allocates a new <code>Thread</code> object.</TD></TR></TABLE> <!-- ========== METHOD SUMMARY =========== --><A NAME="method_summary"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TD COLSPAN=2><FONT SIZE="+2"><B>Method Summary</B></FONT></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>static int</CODE></FONT></TD><TD><CODE><B><A HREF="../../java/lang/Thread.html#activeCount()">activeCount</A></B>()</CODE><BR> Returns the current number of active threads in this thread's thread group.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE> void</CODE></FONT></TD><TD><CODE><B><A HREF="../../java/lang/Thread.html#checkAccess()">checkAccess</A></B>()</CODE><BR> Determines if the currently running thread has permission to modify this thread.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE> int</CODE></FONT></TD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -