java 多線程系列基礎篇(三)之start()和run()的區別

來源:互聯網
上載者:User

標籤:是什麼   syn   cpu   catch   void   status   extend   ati   gpo   

概要

Thread類包含start()和run()方法,它們的區別是什嗎?本章將對此作出解答。

start() 和 run()的區別說明

start() : 它的作用是啟動一個新線程,新線程會執行相應的run()方法。start()不能被重複調用。
run()   : run()就和普通的成員方法一樣,可以被重複調用。單獨調用run()的話,會在當前線程中執行run(),而並不會啟動新線程!

 

下面以代碼來進行說明。

class MyThread extends Thread{      public void run(){        ...    } };MyThread mythread = new MyThread();

mythread.start()會啟動一個新線程,並在新線程中運行run()方法。
而mythread.run()則會直接在當前線程中運行run()方法,並不會啟動一個新線程來運行run()。

 

start() 和 run()的區別樣本

下面,通過一個簡單樣本示範它們之間的區別。源碼如下:

 1 // Demo.java 的源碼 2 class MyThread extends Thread{   3     public MyThread(String name) { 4         super(name); 5     } 6  7     public void run(){ 8         System.out.println(Thread.currentThread().getName()+" is running"); 9     } 10 }; 11 12 public class Demo {  13     public static void main(String[] args) {  14         Thread mythread=new MyThread("mythread");15 16         System.out.println(Thread.currentThread().getName()+" call mythread.run()");17         mythread.run();18 19         System.out.println(Thread.currentThread().getName()+" call mythread.start()");20         mythread.start();21     }  22 }

運行結果:

main call mythread.run()main is runningmain call mythread.start()mythread is running

結果說明:
(01) Thread.currentThread().getName()是用於擷取“當前線程”的名字。當前線程是指正在cpu中調度執行的線程。
(02) mythread.run()是在“主線程main”中調用的,該run()方法直接運行在“主線程main”上。
(03) mythread.start()會啟動“線程mythread”,“線程mythread”啟動之後,會調用run()方法;此時的run()方法是運行在“線程mythread”上。

 

start() 和 run()相關源碼(基於JDK1.7.0_40)

Thread.java中start()方法的源碼如下:

public synchronized void start() {    // 如果線程不是"就緒狀態",則拋出異常!    if (threadStatus != 0)        throw new IllegalThreadStateException();    // 將線程添加到ThreadGroup中    group.add(this);    boolean started = false;    try {        // 通過start0()啟動線程        start0();        // 設定started標記        started = true;    } finally {        try {            if (!started) {                group.threadStartFailed(this);            }        } catch (Throwable ignore) {        }    }}

說明:start()實際上是通過本地方法start0()啟動線程的。而start0()會新運行一個線程,新線程會調用run()方法。

private native void start0();

 

Thread.java中run()的代碼如下:

public void run() {    if (target != null) {        target.run();    }}

說明:target是一個Runnable對象。run()就是直接調用Thread線程的Runnable成員的run()方法,並不會建立一個線程。

轉載:http://www.cnblogs.com/skywang12345/p/3479083.html

java 多線程系列基礎篇(三)之start()和run()的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.