1. 程式人生 > 實用技巧 >多執行緒建立的三種方式

多執行緒建立的三種方式

process 程序
thread 執行緒

1.程式、程序、執行緒

程式是指令和資料的有序集合,它沒有任何執行的含義,是靜態
程序是程式的一次執行,是動態的,是系統資源分配的單位
程序裡面包含多個執行緒,一個程序裡面包含至少一個執行緒,執行緒是CPU呼叫和執行的基本單位。

真正的多執行緒是有多個CPU同時執行多個執行緒喲(如伺服器),現實中有很多模擬出來的多執行緒,只有一個CPU,在同那個一個時間點,CPU只能執行一個程式碼,
但由於CPU在多個執行緒之間切換的太快,就產生了同時執行的錯覺。

2.執行緒的建立方式(有三種)

2.1第一種,繼承Thread類


1.建立一個執行緒類,繼承Thread類

2.重寫run()方法
3.建立執行緒類物件,物件呼叫start()方法開啟執行緒
注意:執行緒開啟不一定立即執行,由CPU排程執行。

不建議使用,避免OOP單繼承的侷限性

/*
* 1.繼承Thread類
* 2.重寫run()方法
* 3.建立物件,呼叫start()方法,開啟執行緒*/
public class TestThread extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("我自己開創的執行緒"+i);
        }
    }

    
public static void main(String[] args) { TestThread testThread = new TestThread(); testThread.start(); for (int i = 0; i < 1000; i++) { System.out.println("main程序"+i); } } }

2.2第二種,實現Runable介面


1.建立一個執行緒類,實現Runnable介面
2.實現run()方法
3.建立執行緒類物件,new Thread(物件).start()來啟動執行緒(代理)(自己建立的執行緒無start()方法)

推薦使用:避免單繼承的侷限性,靈活方便,方便同一個物件被多個執行緒所使用。

public class TestThread03 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("自創執行緒"+i);
        }
    }

    public static void main(String[] args) {
        TestThread03 testThread03 = new TestThread03();
        new Thread(testThread03).start();//這裡與第一種方法不同

        for (int i = 0; i < 500; i++) {
            System.out.println("main執行緒"+i);
        }

    }
}

當多個執行緒在搶佔同一個資源時,如果不加以處理,會出現多個執行緒同時使用同一個資源的情況,發生資源資料紊亂了。--執行緒併發問題

2.3建立執行緒的第三種方式:利用callable介面


* 1.繼承callable介面
* 2.重寫call()方法,含返回值型別和丟擲異常
* 3.建立類物件
* 4.建立執行任務 ExecutorService service = Executors.newFixedThreadPool(3);
* 5。提交執行 Future<Boolean> f1 = service.submit(t1);
* 6.獲取結果 boolean rs1 = f1.get();
* 7.關閉服務 service.shutdownNow();

/*
* 建立執行緒的第三種方式:利用callable介面
* 1.繼承callable介面
* 2.重寫call()方法,含返回值型別和丟擲異常
* 3.建立類物件
* 4.建立執行任務 ExecutorService service = Executors.newFixedThreadPool(3);
* 5。提交執行  Future<Boolean> f1 = service.submit(t1);
* 6.獲取結果  boolean rs1 = f1.get();
* 7.關閉服務 service.shutdownNow();
* */

import java.util.concurrent.*;

public class TestCallable implements Callable {
    @Override
    public Boolean call() throws Exception {
        System.out.println("自創執行緒開始執行。。。。。");
        return true;
    }

    public static void main(String[] args) {
        TestCallable t1 = new TestCallable();
        TestCallable t2 = new TestCallable();
        TestCallable t3 = new TestCallable();
        //建立執行服務
      ExecutorService service = Executors.newFixedThreadPool(3);
      //提交執行
      Future<Boolean> f1 = service.submit(t1);
      Future<Boolean> f2 = service.submit(t2);
      Future<Boolean> f3 = service.submit(t3);

      //獲取結果
        try {
            boolean rs1 = f1.get();
            boolean rs2 = f2.get();
            boolean rs3 = f3.get();
            System.out.println(rs1+" "+rs2+" "+rs3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

//        關閉服務
        service.shutdownNow();



    }
}