📄 java08_02.htm
字号:
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Java程序设计</title>
</head>
<body background="Bg.gif">
<p align="center"><font size="5"><b>§8.2创建线程</b></font></p>
<p align="left"> 在Java中,有两种方法可以创建线程,一是继承Thread类,一是实现Runnable接口。但不管采用哪种方法,都要用到Java类库中的Thread类以及相关方法。下表是它的一些常用方法:</p>
<table cellSpacing="0" cellPadding="3" width="100%" summary border="1">
<tbody>
<tr class="TableHeadingColor" bgColor="#ccccff">
<td><font size="+2"><b>构造方法</b></font></td>
</tr>
<tr class="TableRowColor" >
<td><code><b>Thread</b>()<br>
</code> 构造一个线程对象</td>
</tr>
<tr class="TableRowColor" >
<td><code><b>Thread</b>(Runnable target)</code><br>
构造一个线程对象,target是被创建线程的目标对象,它实现了Runnable接口中的run()方法</td>
</tr>
<tr class="TableRowColor" >
<td><code><b>Thread</b>(String name)</code><br>
用制定字符串名构造一个新线程</td>
</tr>
<tr class="TableRowColor" >
<td><code><b>Thread</b>(ThreadGroup group, Runnable target)</code><br>
在指定线程组中构造一个新线程,使用目标对象target的run()方法</td>
</tr>
<tr class="TableRowColor" >
<td><code><b>Thread</b>(Runnable target, String name)</code><br>
Allocates a
new <code>Thread</code> object.</td>
</tr>
<tr class="TableRowColor" >
<td><code><b>Thread</b>(ThreadGroup group, Runnable target,
String 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 class="TableRowColor" >
<td><code><b>Thread</b>(ThreadGroup group, Runnable target,
String name, long stackSize)</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, belongs to
the thread group referred to by <code>group</code>, and has the
specified <i>stack size</i>.</td>
</tr>
</tbody>
</table>
<!-- ========== METHOD SUMMARY =========== -->
<a name="method_summary"><!-- -->
</a>
<table cellSpacing="0" cellPadding="3" width="100%" summary border="1">
<tbody>
<tr class="TableHeadingColor" bgColor="#ccccff">
<td colSpan="2"><font size="+2"><b>Method Summary</b></font></td>
</tr>
<tr class="TableRowColor" >
<td vAlign="top" align="right" width="1%"><font size="-1"><code>static int</code></font></td>
<td><code><b>activeCount</b>()</code><br>
Returns the
number of active threads in the current thread's thread group.</td>
</tr>
<tr class="TableRowColor" >
<td vAlign="top" align="right" width="1%"><font size="-1"><code> void</code></font></td>
<td><code><b>checkAccess</b>()</code><br>
Determines
if the currently running thread has permission to modify this thread.</td>
</tr>
<tr class="TableRowColor" >
<td vAlign="top" align="right" width="1%"><font size="-1"><code> int</code></font></td>
<td><code><b>countStackFrames</b>()</code><br>
<b>Deprecated.</b> <i>The
definition of this call depends on <code>suspend()</code>,
which is deprecated. Further, the results of this call were never
well-defined.</i></td>
</tr>
<tr class="TableRowColor" >
<td vAlign="top" align="right" width="1%"><font size="-1"><code>static Thread</code></font></td>
<td><code><b>currentThread</b>()</code><br>
判断当前哪个线程正在执行</td>
</tr>
<tr class="TableRowColor" >
<td vAlign="top" align="right" width="1%"><font size="-1"><code> void</code></font></td>
<td><code><b>destroy</b>()</code><br>
Destroys
this thread, without any cleanup.</td>
</tr>
<tr class="TableRowColor" >
<td vAlign="top" align="right" width="1%"><font size="-1"><code>static void</code></font></td>
<td><code><b>dumpStack</b>()</code><br>
Prints a
stack trace of the current thread.</td>
</tr>
<tr class="TableRowColor" >
<td vAlign="top" align="right" width="1%"><font size="-1"><code>static int</code></font></td>
<td><code><b>enumerate</b>(Thread[] tarray)</code><br>
Copies into
the specified array every active thread in the current thread's thread
group and its subgroups.</td>
</tr>
<tr class="TableRowColor" >
<td vAlign="top" align="right" width="1%"><font size="-1"><code> ClassLoader</code></font></td>
<td><code><b>getContextClassLoader</b>()</code><br>
Returns the
context ClassLoader for this Thread.</td>
</tr>
<tr class="TableRowColor" >
<td vAlign="top" align="right" width="1%"><font size="-1"><code> String</code></font></td>
<td><code><b>getName</b>()</code><br>
返回线程的名字</td>
</tr>
<tr class="TableRowColor" >
<td vAlign="top" align="right" width="1%"><font size="-1"><code> int</code></font></td>
<td><code><b>getPriority</b>()</code><br>
获取线程的优先级</td>
</tr>
<tr class="TableRowColor" >
<td vAlign="top" align="right" width="1%"><font size="-1"><code> ThreadGroup</code></font></td>
<td><code><b>getThreadGroup</b>()</code><br>
Returns the
thread group to which this thread belongs.</td>
</tr>
<tr class="TableRowColor" >
<td vAlign="top" align="right" width="1%"><font size="-1"><code>static boolean</code></font></td>
<td><code><b>holdsLock</b>(Object obj)</code><br>
Returns <tt>true</tt>
if and only if the current thread holds the monitor lock on the
specified object.</td>
</tr>
<tr class="TableRowColor" >
<td vAlign="top" align="right" width="1%"><font size="-1"><code> void</code></font></td>
<td><code><b>interrupt</b>()</code><br>
Interrupts
this thread.</td>
</tr>
<tr class="TableRowColor" >
<td vAlign="top" align="right" width="1%"><font size="-1"><code>static boolean</code></font></td>
<td><code><b>interrupted</b>()</code><br>
Tests
whether the current thread has been interrupted.</td>
</tr>
<tr class="TableRowColor" >
<td vAlign="top" align="right" width="1%"><font size="-1"><code> boolean</code></font></td>
<td><code><b>isAlive</b>()</code><br>
测试线程是否已经死亡。</td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -