⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java08_02.htm

📁 JAVA的课件
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;构造一个线程对象</td>
    </tr>
    <tr class="TableRowColor" >
      <td><code><b>Thread</b>(Runnable&nbsp;target)</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;构造一个线程对象,target是被创建线程的目标对象,它实现了Runnable接口中的run()方法</td>
    </tr>
    <tr class="TableRowColor" >
      <td><code><b>Thread</b>(String&nbsp;name)</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;用制定字符串名构造一个新线程</td>
    </tr>
    <tr class="TableRowColor" >
      <td><code><b>Thread</b>(ThreadGroup&nbsp;group, Runnable&nbsp;target)</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;在指定线程组中构造一个新线程,使用目标对象target的run()方法</td>
    </tr>
    <tr class="TableRowColor" >
      <td><code><b>Thread</b>(Runnable&nbsp;target, String&nbsp;name)</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Allocates a 
        new <code>Thread</code> object.</td>
    </tr>
    <tr class="TableRowColor" >
      <td><code><b>Thread</b>(ThreadGroup&nbsp;group, Runnable&nbsp;target, 
        String&nbsp;name)</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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&nbsp;group, Runnable&nbsp;target, 
        String&nbsp;name, long&nbsp;stackSize)</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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>
&nbsp; <!-- ========== 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&nbsp;int</code></font></td>
      <td><code><b>activeCount</b>()</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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>&nbsp;void</code></font></td>
      <td><code><b>checkAccess</b>()</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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>&nbsp;int</code></font></td>
      <td><code><b>countStackFrames</b>()</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Deprecated.</b>&nbsp;<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&nbsp;Thread</code></font></td>
      <td><code><b>currentThread</b>()</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;判断当前哪个线程正在执行</td>
    </tr>
    <tr class="TableRowColor" >
      <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;void</code></font></td>
      <td><code><b>destroy</b>()</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Destroys   
        this thread, without any cleanup.</td>  
    </tr>
    <tr class="TableRowColor" >
      <td vAlign="top" align="right" width="1%"><font size="-1"><code>static&nbsp;void</code></font></td>
      <td><code><b>dumpStack</b>()</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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&nbsp;int</code></font></td>
      <td><code><b>enumerate</b>(Thread[]&nbsp;tarray)</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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>&nbsp;ClassLoader</code></font></td>
      <td><code><b>getContextClassLoader</b>()</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the   
        context ClassLoader for this Thread.</td>  
    </tr>
    <tr class="TableRowColor" >
      <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;String</code></font></td>
      <td><code><b>getName</b>()</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;返回线程的名字</td>
    </tr>
    <tr class="TableRowColor" >
      <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;int</code></font></td>
      <td><code><b>getPriority</b>()</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;获取线程的优先级</td>
    </tr>
    <tr class="TableRowColor" >
      <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;ThreadGroup</code></font></td>
      <td><code><b>getThreadGroup</b>()</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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&nbsp;boolean</code></font></td>
      <td><code><b>holdsLock</b>(Object&nbsp;obj)</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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>&nbsp;void</code></font></td>
      <td><code><b>interrupt</b>()</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Interrupts   
        this thread.</td>  
    </tr>
    <tr class="TableRowColor" >
      <td vAlign="top" align="right" width="1%"><font size="-1"><code>static&nbsp;boolean</code></font></td>
      <td><code><b>interrupted</b>()</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tests   
        whether the current thread has been interrupted.</td>  
    </tr>
    <tr class="TableRowColor" >
      <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;boolean</code></font></td>
      <td><code><b>isAlive</b>()</code><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;测试线程是否已经死亡。</td> 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -