Java線程概念以及三種實現的方式

前言

從本章開始,我們將展開對多線程的學習,多線程的內容比較多,將會挑選主要的類進行分析,本章主要是帶大家回顧下線程的基礎知識。

1、線程的狀態

我們將由一張圖,來看看線程由哪些狀態
在這裏插入圖片描述
說明:
線程共包括以下5種狀態。

  1. 新建狀態(New) : 線程對象被創建後,就進入了新建狀態。例如,Thread thread = new Thread()。
  2. 就緒狀態(Runnable): 也被稱爲“可執行狀態”。線程對象被創建後,其它線程調用了該對象的start()方法,從而來啓動該線程。例如,thread.start()。處於就緒狀態的線程,隨時可能被CPU調度執行。
  3. 運行狀態(Running) : 線程獲取CPU權限進行執行。需要注意的是,線程只能從就緒狀態進入到運行狀態。
  4. 阻塞狀態(Blocked) : 阻塞狀態是線程因爲某種原因放棄CPU使用權,暫時停止運行。直到線程進入就緒狀態,纔有機會轉到運行狀態。阻塞的情況分三種:
    (01) 等待阻塞 – 通過調用線程的wait()方法,讓線程等待某工作的完成。
    (02) 同步阻塞 – 線程在獲取synchronized同步鎖失敗(因爲鎖被其它線程所佔用),它會進入同步阻塞狀態。
    (03) 其他阻塞 – 通過調用線程的sleep()或join()或發出了I/O請求時,線程會進入到阻塞狀態。當sleep()狀態超時、join()等待線程終止或者超時、或者I/O處理完畢時,線程重新轉入就緒狀態。
  5. 死亡狀態(Dead) : 線程執行完了或者因異常退出了run()方法,該線程結束生命週期。

2、線程的實現方式

2.1、繼承Thread類

下面我們來看一段測試代碼:

public class ExtendsThread extends Thread {

    public ExtendsThread(String name){
        super(name);
    }

    @Override
    public void run() {
        System.out.println(this.getName());
    }

    public static void main(String[] args){
        Thread t1=new ExtendsThread("Thread_1");
        Thread t2=new ExtendsThread("Thread_2");
        t1.start();
        t2.start();
    }
}

結果:

Thread_1
Thread_2
2.2、實現Runnable接口
public class TestRunnable {
    public static void main(String[] args) {
        RunnableImpl runnable1 = new RunnableImpl();
        RunnableImpl runnable2 = new RunnableImpl();
        Thread thread1 = new Thread(runnable1,"thread_1");
        Thread thread2 = new Thread(runnable2, "thread_2");
        thread1.start();
        thread2.start();
    }
}

class RunnableImpl implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}

執行結果:

thread_2
thread_1

2.3、使用Callable和Future接口創建線程

public class ThreadTest {
    public static void main(String[] args) {

       Callable<Integer> myCallable = new MyCallable();    // 創建MyCallable對象
       FutureTask<Integer> ft = new FutureTask<Integer>(myCallable); //使用FutureTask來包裝MyCallable對象
       for (int i = 0; i < 15; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
            if (i == 2) {
                  Thread thread = new Thread(ft);   //FutureTask對象作爲Thread對象的target創建新的線程
                  thread.start();                      //線程進入到就緒狀態
            }
        }
        System.out.println("主線程for循環執行完畢..");
        try {
            int sum = ft.get();            //取得新創建的新線程中的call()方法返回的結果
            System.out.println("sum = " + sum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

class MyCallable implements Callable<Integer> {
    // 與run()方法不同的是,call()方法具有返回值
    @Override
    public Integer call() {
        int sum = 0;
        for (int i=0; i<8; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
            sum += i;
        }
        return sum;
    }
}

執行結果:

main 0
main 1
main 2
main 3
main 4
main 5
main 6
main 7
main 8
Thread-0 0
main 9
Thread-0 1
main 10
Thread-0 2
main 11
Thread-0 3
main 12
main 13
main 14
主線程for循環執行完畢..
Thread-0 4
Thread-0 5
Thread-0 6
Thread-0 7
sum = 28

上面的結果永遠都是sum=28在最後,那麼爲什麼sum =28會永遠最後輸出呢?
原因在於通過ft.get()方法獲取子線程call()方法的返回值時,當子線程此方法還未執行完畢,ft.get()方法會一直阻塞,直到call()方法執行完畢才能取到返回值。

結束語

本篇先介紹到這,後面將詳細介紹Callable和Future接口。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章