国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java中啟動線程start和run的兩種方法

Java中啟動線程start和run的兩種方法

2021-02-02 11:55來離 Java教程

start()方法它的作用是啟動一個新線程,run()就和普通的成員方法一樣,可以被重復調用。接下來通過本文給大家分享Java中啟動線程start和run的兩種方法,需要的朋友參考下吧

一、區別

Java中啟動線程有兩種方法,繼承Thread類和實現Runnable接口,由于Java無法實現多重繼承,所以一般通過實現Runnable接口來創建線程。但是無論哪種方法都可以通過start()和run()方法來啟動線程,下面就來介紹一下他們的區別。

start方法:

通過該方法啟動線程的同時也創建了一個線程,真正實現了多線程。無需等待run()方法中的代碼執行完畢,就可以接著執行下面的代碼。此時start()的這個線程處于就緒狀態,當得到CPU的時間片后就會執行其中的run()方法。這個run()方法包含了要執行的這個線程的內容,run()方法運行結束,此線程也就終止了。

run方法:

通過run方法啟動線程其實就是調用一個類中的方法,當作普通的方法的方式調用。并沒有創建一個線程,程序中依舊只有一個主線程,必須等到run()方法里面的代碼執行完畢,才會繼續執行下面的代碼,這樣就沒有達到寫線程的目的。

下面我們通過一個很經典的題目來理解一下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Test {
  public static void main(String[] args) {
    Thread t = new Thread(){
      public void run() {
        pong();
      }
    };
    t.run();
    System.out.println("ping");
  }
 
  static void pong() {
    System.out.println("pong");
  }
}

代碼如圖所示,那么運行程序,輸出的應該是什么呢?沒錯,輸出的是”pong ping”。因為t.run()實際上就是等待執行new Thread里面的run()方法調用pong()完畢后,再繼續打印”ping”。它不是真正的線程。

而如果我們將t.run();修改為t.start();那么,結果很明顯就是”ping pong”,因為當執行到此處,創建了一個新的線程t并處于就緒狀態,代碼繼續執行,打印出”ping”。此時,執行完畢。線程t得到CPU的時間片,開始執行,調用pong()方法打印出”pong”。

如果感興趣,還可以多加幾條語句自己看看效果。

二、源碼

那么他們本質上的區別在哪里,我們來看一下源碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**java
   * Causes this thread to begin execution; the Java Virtual Machine
   * calls the <code>run</code> method of this thread.
   * <p>
   * The result is that two threads are running concurrently: the
   * current thread (which returns from the call to the
   * <code>start</code> method) and the other thread (which executes its
   * <code>run</code> method).
   * <p>
   * It is never legal to start a thread more than once.
   * In particular, a thread may not be restarted once it has completed
   * execution.
   *
   * @exception IllegalThreadStateException if the thread was already
   *        started.
   * @see    #run()
   * @see    #stop()
   */
  public synchronized void start() {
    /**
     * This method is not invoked for the main method thread or "system"
     * group threads created/set up by the VM. Any new functionality added
     * to this method in the future may have to also be added to the VM.
     *
     * A zero status value corresponds to state "NEW".
     */
    if (threadStatus != 0)
      throw new IllegalThreadStateException();
    /* Notify the group that this thread is about to be started
     * so that it can be added to the group's list of threads
     * and the group's unstarted count can be decremented. */
    group.add(this);
    boolean started = false;
    try {
      start0();
      started = true;
    } finally {
      try {
        if (!started) {
          group.threadStartFailed(this);
        }
      } catch (Throwable ignore) {
        /* do nothing. If start0 threw a Throwable then
         it will be passed up the call stack */
      }
    }
  }
  private native void start0();

可以看到,當一個線程啟動的時候,它的狀態(threadStatus)被設置為0,如果不為0,則拋出IllegalThreadStateException異常。正常的話,將該線程加入線程組,最后嘗試調用start0方法,而start0方法是私有的native方法(Native Method是一個java調用非java代碼的接口)。

我猜測這里是用C實現的,看來調用系統底層還是要通過C語言。這也就是為什么start()方法可以實現多線程。而調用run()方法,其實只是調用runnable里面自己實現的run()方法。

我們再看看Thread里run()的源碼:

?
1
2
3
4
5
6
@Override
  public void run() {
    if (target != null) {
      target.run();
    }
  }

如果target不為空,則調用target的run()方法,那么target是什么:

?
1
2
/* What will be run. */
  private Runnable target;

其實就是一個Runnable接口,正如上面代碼中new Thread的部分,其實我們就是在實現它的run()方法。所以如果直接調用run,就和一個普通的方法沒什么區別,是不會創建新的線程的,因為壓根就沒執行start0方法。

三、實現

前面說了,繼承Thread類和實現Runnable接口都可以定義一個線程,那么他們又有什么區別呢?
在《Java核心技術卷1 第9版》第627頁提到。可以通過一下代碼構建Thread的子類定義一個線程:

?
1
2
3
4
5
class MyThread extends Thread {
  public void run() {
    //do Something
  }
}

然后,實例化一個對象,調用其start方法。不過這個方法不推薦。應該減少需要并行運行的任務數量。如果任務很多,要為每個任務創建一個獨立的線程所付出的代價太多,當然可以用線程池來解決。

實現Runnable接口所具有的優勢:

1、避免Java單繼承的問題

2、適合多線程處理同一資源

3、代碼可以被多線程共享,數據獨立,很容易實現資源共享

總結一下:

1.start() 可以啟動一個新線程,run()不能

2.start()不能被重復調用,run()可以

3.start()中的run代碼可以不執行完就繼續執行下面的代碼,即進行了線程切換。直接調用run方法必須等待其代碼全部執行完才能繼續執行下面的代碼。

4.start() 實現了多線程,run()沒有實現多線程。

以上所述是小編給大家介紹的Java中啟動線程start和run方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:http://blog.csdn.net/lai_li/article/details/53070141?locationNum=13&fps=1

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 一级片大片| 国产在线中文字幕 | 国产精品一区二区不卡 | 大象一区 | 久久久天堂国产精品女人 | 婷婷色综合 | 欧美日韩第一页 | 国产精品久久国产精品 | 欧美精三区欧美精三区 | 成人综合激情 | 色视频免费在线 | 一区二区三区免费观看视频 | 动漫精品一区二区三区 | 国产一区精品在线 | 欧美性猛交xxxx黑人猛交 | 日韩国产欧美视频 | 久久精品美女 | 中国女人真人一级毛片 | 国产精品久久久久久久久免费高清 | 成人免费一区二区三区视频网站 | 久久精品亚洲精品 | 在线91| 久久9国产偷伦 | 亚洲国产成人av好男人在线观看 | 一级片 | 日日爱视频 | 午夜视频导航 | 午夜电影网址 | 97久久精品 | 日韩精品中文字幕在线 | 欧美日韩久久精品 | 久久久毛片 | 日韩免费| 99国产精品久久久久久久久久 | 97成人在线免费视频 | 日韩精品视频在线播放 | 欧美成人观看 | 久久中文字幕在线观看 | 日韩一区二区三区视频 | 特级西西人体444www高清大胆 | 亚洲激情 欧美 |