java中获取线程返回值的几种方式

在之前的工作中,主要是写业务代码,线程使用得很少,也没有考虑执行线程后如何获取返回值的问题,而线程中的run()方法是没有返回值的。那如果在线程执行完毕后想要获取对应的值要怎么做?借着最近的学习总结一下。线程返回值主要有几种方式。

  1. 通过代码做循环判断
  2. 使用join
  3. 使用Callable接口和FutureTask
  4. 使用线程池

下面通过demo做一下演示

public class MyThreadReturn implements Runnable {
    /** 模拟线程执行完毕后主程序要获取的值*/
    private String returnValue;
    @Override
    public void run() {
        System.out.println("线程执行......");
        /** 模拟IO*/
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程执行完毕......");
        returnValue = "hello world!!!";
    }
    public String getReturnValue(){
        return returnValue;
    }

    public static void main(String[] args) {
        MyThreadReturn myThreadReturn = new MyThreadReturn();
        Thread thread = new Thread(myThreadReturn);
        thread.start();
        System.out.println(myThreadReturn.getReturnValue());
    }
}

以上代码因为MyThreadReturn线程需要5秒执行完毕,主线程中并不能顺利获取到returnValue

null
线程执行.....

将代码做如下修改

通过循环判断

该方法本质是自己控制业务逻辑

public static void main(String[] args) throws InterruptedException {
        MyThreadReturn myThreadReturn = new MyThreadReturn();
        Thread thread = new Thread(myThreadReturn);
        thread.start();
        /** 通过while循环判断*/
        while (myThreadReturn.getReturnValue() == null){
            Thread.sleep(1000);
        }
        System.out.println(myThreadReturn.getReturnValue());
    }

使用join

使用join方法可以让子线程执行完毕后再执行主线程,本质和通过循环判断一样

public static void main(String[] args) throws InterruptedException {
        MyThreadReturn myThreadReturn = new MyThreadReturn();
        Thread thread = new Thread(myThreadReturn);
        thread.start();
        /** 使用join*/
        thread.join();
        System.out.println(myThreadReturn.getReturnValue());
    }

使用Callable接口和FutureTask

代码如下,通过FutureTask的get()方法获取返回值

public class MyCallable implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println("线程执行......");
        Thread.sleep(5000);
        System.out.println("线程执行完毕......");
        return "hello world!!!";
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> futureTask = new FutureTask<>(new MyCallable());
        /***
         * futureTask 实现了 Runnable接口
         * 所以新建线程的时候可以传入futureTask
         * FutureTask重写的run方法中实际是调用了Callable接口在call()方法
         * 所以执行线程的时候回执行call方法的内容
         */
        Thread thread = new Thread(futureTask);
        thread.start();
        String value = futureTask.get();
        System.out.println(value);
    }
}

使用线程池

public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Future<String> submit = executorService.submit(new MyCallable());
        System.out.println(submit.get());
    }

以上代码返回值均为

线程执行......
线程执行完毕......
hello world!!!
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 进程和线程 进程 所有运行中的任务通常对应一个进程,当一个程序进入内存运行时,即变成一个进程.进程是处于运行过程中...
    胜浩_ae28阅读 5,137评论 0 23
  • Java-Review-Note——4.多线程 标签: JavaStudy PS:本来是分开三篇的,后来想想还是整...
    coder_pig阅读 1,672评论 2 17
  • Android Handler机制系列文章整体内容如下: Android Handler机制1之ThreadAnd...
    隔壁老李头阅读 4,281评论 2 12
  • 一、并发 进程:每个进程都拥有自己的一套变量 线程:线程之间共享数据 1.线程 Java中为多线程任务提供了很多的...
    SeanMa阅读 2,522评论 0 11
  • 于我看来,人是由感性和理性组成的。理性的现实意义高于感性,但人总会被一时迸发出的感性操控。我是极不喜欢这样的,这种...
    长风雅阅读 498评论 0 4