Java並發編程:線程的建立

來源:互聯網
上載者:User

標籤:ack   lis   rgs   contain   erer   art   task   str   src   

Java並發編程:線程的建立Table of Contents
  • 1. Thread
  • 2. Runnable
  • 3. start() 和 run()

在Java中線程的建立主要有兩種,一種是通過繼承抽象類別Thread,一種是通過實現Runnable介面。當然,還有Concurent包裡面的Callable和Future也可以算是一種。

1 Thread

我們先來看一下,使用Thread如何建立線程:

public class ThreadDemo extends Thread {    @Override    public void run() {        System.out.println("Current thread name is: " + Thread.currentThread().getName());    }    public static void main(String[] args) {        for (int i = 0; i < 10; i++) {            Thread thread = new ThreadDemo();            thread.start();        }    }}
Current thread name is: Thread-0Current thread name is: Thread-2Current thread name is: Thread-1Current thread name is: Thread-3Current thread name is: Thread-5Current thread name is: Thread-4Current thread name is: Thread-6Current thread name is: Thread-7Current thread name is: Thread-8Current thread name is: Thread-9

可以看到,我們建立來10個線程,但是執行的順序是不確定的。可以設定優先權,預設是5,最大是10,最小是1.但是即使設定來優先順序,順序也是不能保證。

public static void main(String[] args) {    for (int i = 0; i < 10; i++) {        Thread thread = new ThreadDemo();        thread.setPriority(i + 1);        thread.start();    }}

優先順序逐漸上升,但執行但結果還是一樣。

2 Runnable

我們再來使用Runnable建立線程:

public class RunnableDemo implements Runnable {    @Override    public void run() {        System.out.println("Current runnable thread name is: " + Thread.currentThread().getName());    }    public static void main(String[] args) {        for (int i = 0; i < 10; i++) {            Thread thread = new Thread(new RunnableDemo());            thread.start();        }    }}
Current runnable thread name is: Thread-0Current runnable thread name is: Thread-3Current runnable thread name is: Thread-2Current runnable thread name is: Thread-5Current runnable thread name is: Thread-1Current runnable thread name is: Thread-6Current runnable thread name is: Thread-7Current runnable thread name is: Thread-4Current runnable thread name is: Thread-8Current runnable thread name is: Thread-9

可以看出,實現Runnable介面介面之後但RunnableDemo類但執行個體還是無法直接啟動並執行,它必須將執行個體對象傳入Thread類,然後,才能調用Thread對象中的start()進行啟動。

3 start() 和 run()

我們接下來來看一下start() 和 run()的區別:
當我們中定義新的線程類的時候,唯一覆寫的方法就是run()。那麼,我們能不能直接調用run()方法呢?
答案是肯定的。
我們將start()替換為run()再試一下:

public static void main(String[] args) {    for (int i = 0; i < 10; i++) {        Thread thread = new Thread(new RunnableDemo());        thread.run();    }}
Current runnable thread name is: mainCurrent runnable thread name is: mainCurrent runnable thread name is: mainCurrent runnable thread name is: mainCurrent runnable thread name is: mainCurrent runnable thread name is: mainCurrent runnable thread name is: mainCurrent runnable thread name is: mainCurrent runnable thread name is: mainCurrent runnable thread name is: main

結果全是主線程在運行,也就是說根據就沒有新的線程啟動運行。所以,使用run()方法調用時,就和一般的函數調用一樣,是由當前線程進行調用的,並不會啟動新的線程,然後在新的線程中運行這個run()方法。只有使用start()方法去調用,才會啟動新的線程,然後,在新的線程中運行run()方法。

Date: 2017-07-04 21:37

Author: WEN YANG

Created: 2017-07-04 Tue 22:44

Emacs 25.2.1 (Org mode 8.2.10)

Validate

Java並發編程:線程的建立

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.