1. 程式人生 > >【Java】執行緒的建立方式

【Java】執行緒的建立方式

 

1、繼承Thread類方式

這種方式適用於執行特定任務,並且需要獲取處理後的資料的場景。

舉例:一個用於累加陣列內資料的和的執行緒。

public class AdditionThread extends Thread {
    private int sum = 0;
    private int[] nums;
​
    public AdditionThread(int[] nums, String threadName) {
        super(threadName);
        this.nums = nums;
    }
​
    @Override
    public void run() {
        for (int num : nums) {
            sum += num;
        }
    }
​
    public int getSum() {
        return sum;
    }
}

呼叫方式:

public class Main {
    public static void main(String[] args) throws InterruptedException {
        int[] nums = {10, 12, 15, 200, 100};
        AdditionThread thread = new AdditionThread(nums, "AdditionThread");
        thread.start();
        thread.join();
​
        System.out.println("sum=" + thread.getSum());
    }
}

 

2、Runnable 介面方式

定義一個實現Runnable介面的類,或者直接建立一個匿名內部類,並覆蓋 run() 方法。最後作為引數傳給Thread的建構函式。

public class Main {
    public static void main(String[] args) {
        // 自定義的 Runnable
        Runnable runnable = new MyRunnable();
        Thread thread = new Thread(runnable, "Runnable-Thread");
        thread.start();
​
        // 自定義匿名內部類
        new Thread(() -> {
            System.out.println("Inner class");
        }).start();
    }
​
    static class MyRunnable implements Runnable {
        @Override
        public void run() {
            System.out.println("MyRunnable");
        }
    }
}

 

3、 Callable 介面方式

Callable 介面與 Runnable 介面的區別:

(1)Callable 的方法為call(),Runnable的方法為run()。

(2)Callable 的方法由返回值,Runnable 沒有。

(3)Callable 的方法宣告的Exception,Runnable的沒有。

public class Main {
    public static void main(String[] args) {
        MyCallable myCallable = new MyCallable();
        FutureTask<String> task = new FutureTask<>(myCallable);
        Thread thread = new Thread(task, "FutureTask");
        thread.start();
        try {
            // 通過get方法獲取返回值
            String result = task.get();
            System.out.println(result);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
​
    static class MyCallable implements Callable<String> {
        @Override
        public String call() throws Exception {
            // 模擬超時操作
            Thread.sleep(1000);
            return "OK";
        }
    }
}

 

4、執行緒池方式

我們可以通過 ThreadPoolExecutor 類的建構函式來建立執行緒池,也可以通過Executors工廠方法來建立,如

// 建立固定執行緒數的執行緒池
Executors.newFixedThreadPool(); 
// 建立只有一個核心執行緒的執行緒池
Executors.newSingleThreadExecutor();
// 建立一個沒有核心執行緒,但可以快取執行緒的執行緒池
Executors.newCachedThreadPool();
// 建立一個適用於執行定時任務的執行緒池
Executors.newScheduledThreadPool();

 

在建立執行緒池時,最好傳入 ThreadFactory 引數,指定執行緒池所建立執行緒的名稱。這樣有利於分析定位可能存在的問題。

public class Main {
    private static final ExecutorService SERVICE =
        Executors.newFixedThreadPool(5, new BasicThreadFactory("My-Thread"));
​
    public static void main(String[] args) {
        // 列印執行緒的名字
        System.out.println("main thread name:" + Thread.currentThread().getName());
        SERVICE.execute(() -> {
            System.out.println("Hello thread pool.");
            // 列印執行緒池裡的執行緒的名字
            System.out.println("thread name:" + Thread.currentThread().getName());
        });
    }
​
    static class BasicThreadFactory implements ThreadFactory {
        private final AtomicInteger threadNumber = new AtomicInteger(0);
        private final String basicName;
​
        public BasicThreadFactory(String basicName) {
            this.basicName = basicName;
        }
​
        @Override
        public Thread newThread(Runnable runnable) {
            Thread thread = new Thread(runnable);
            String name = this.basicName + "-" + threadNumber.incrementAndGet();
            thread.setName(name);
            return thread;
        }
    }
}

&n