首页 > 编程技术 > java

Java并发编程创建并运行线程的方法对比

发布时间:2022-3-18 16:56 作者:字母哥哥

一、创建并运行线程的五种方法

第一种:继承Thread类

这种方式是最基础的一种方式,学过java的朋友都知道,不做赘述。需要注意的是:覆盖实现使用的是run方法,运行线程是start方法。

public class FirstWay extends Thread  {
    @Override
    public void run() {
        System.out.println("第一种实现线程的方式:继承Thread类");
    }
    //模拟测试
    public static void main(String[] args) {
        new FirstWay().start();
    }
}

第二种:实现Runnable接口

第二种实现方式仍然很基础,继承Runnable接口,重写run方法实现线程运行逻辑。需要注意的:运行线程需要套一层new Thread 。

public class SecondWay implements Runnable{
    @Override
    public void run() {
        System.out.println("第二种实现线程的方式:实现Runnable接口");
    }
    //模拟测试
    public static void main(String[] args) {
        new Thread(new SecondWay()).start();
    }
}

第三种:实现Callable接口

第三种方式是实现Callable接口,Callable接口与Runable接口都能实现线程。

public class ThirdWay implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println("第三种实现线程的方式:实现Callable接口");
        return "Callable接口带返回值,可以抛出异常";
    }

    //模拟测试
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> futureTask = new FutureTask<>(new ThirdWay());
        new Thread(futureTask).start();
        //阻塞方法,获取call方法返回值
        System.out.println(futureTask.get());  //打印:Callable接口带返回值,可以抛出异常
    }
}

区别如下:

第四种:线程池 + execute

从JDK5版本开始,java默认提供了线程池的支持,用线程池的方式运行线程可以避免线程的无限扩张导致应用宕机,同时也节省了线程频繁创建与销毁的资源与时间成本。

public class FourthWay implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() +
                ":实现线程的方式Runnable接口,但运行方式不一样,使用线程池");
    }

    public static void main(String[] args) {
        //创建一个固定大小的线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(5);
        for(int i = 0;i < 10;i++){
            threadPool.execute(new FourthWay());
        }
    }
}

线程池ExecutorService使用execute方法运行Runnable接口run方法的线程实现,execute方法与run方法的共同特点是没有返回值。

pool-1-thread-5:实现线程的方式Runnable接口,但运行方式不一样,使用线程池
pool-1-thread-2:实现线程的方式Runnable接口,但运行方式不一样,使用线程池
pool-1-thread-4:实现线程的方式Runnable接口,但运行方式不一样,使用线程池
pool-1-thread-4:实现线程的方式Runnable接口,但运行方式不一样,使用线程池
pool-1-thread-4:实现线程的方式Runnable接口,但运行方式不一样,使用线程池
pool-1-thread-1:实现线程的方式Runnable接口,但运行方式不一样,使用线程池
pool-1-thread-4:实现线程的方式Runnable接口,但运行方式不一样,使用线程池
pool-1-thread-3:实现线程的方式Runnable接口,但运行方式不一样,使用线程池
pool-1-thread-2:实现线程的方式Runnable接口,但运行方式不一样,使用线程池
pool-1-thread-5:实现线程的方式Runnable接口,但运行方式不一样,使用线程池

从上面的结果中可以看出,线程池中包含五个线程。线程运行完成之后并不销毁,而是还回到线程池,下一次执行时从线程池中获取线程资源再次运行。

第五种:线程池 + submit

下面的例子线程池ExecutorService使用submit方法运行Callable接口call方法的线程实现,submit方法与call方法的共同特点是存在返回值。

Future的get方法可以获取call方法的返回值,同时如果call方法抛出异常,Future的get方法也会抛出异常。

public class FifthWay implements Callable<String> {
    @Override
    public String call() throws Exception {
        return Thread.currentThread().getName() + ":Callable接口带返回值,可以抛出异常";
    }

    //模拟测试
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //保存多线程执行结果
        List<String> retList = new ArrayList<>();
        //创建一个固定大小的线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(5);
        for(int i = 0;i < 10;i++){
            Future<String> future = threadPool.submit(new FifthWay());
            retList.add(future.get());
        }
        //java8 语法,打印retlist
        retList.forEach(System.out::println);
    }
}

上文代码中有一个小小的语法糖,retList.forEach(System.out::println);是java8提供的方法引用

pool-1-thread-1:Callable接口带返回值,可以抛出异常
pool-1-thread-2:Callable接口带返回值,可以抛出异常
pool-1-thread-3:Callable接口带返回值,可以抛出异常
pool-1-thread-4:Callable接口带返回值,可以抛出异常
pool-1-thread-5:Callable接口带返回值,可以抛出异常
pool-1-thread-1:Callable接口带返回值,可以抛出异常
pool-1-thread-2:Callable接口带返回值,可以抛出异常
pool-1-thread-3:Callable接口带返回值,可以抛出异常
pool-1-thread-4:Callable接口带返回值,可以抛出异常
pool-1-thread-5:Callable接口带返回值,可以抛出异常

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注猪先飞的更多内容!     

原文出处:https://blog.csdn.net/hanxiaotongtong/article/details/123541

标签:[!--infotagslink--]

您可能感兴趣的文章: