【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;
        }
    }
}
原文地址:https://www.cnblogs.com/laishenghao/p/13965258.html