1. 程式人生 > >Java並發編程:線程的創建

Java並發編程:線程的創建

nds preamble gre container string arc ner set static

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-0
Current thread name is: Thread-2
Current thread name is: Thread-1
Current thread name is: Thread-3
Current thread name is: Thread-5
Current thread name is: Thread-4
Current thread name is: Thread-6
Current thread name is: Thread-7
Current thread name is: Thread-8
Current 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-0
Current runnable thread name is: Thread-3
Current runnable thread name is: Thread-2
Current runnable thread name is: Thread-5
Current runnable thread name is: Thread-1
Current runnable thread name is: Thread-6
Current runnable thread name is: Thread-7
Current runnable thread name is: Thread-4
Current runnable thread name is: Thread-8
Current 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: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current 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並發編程:線程的創建