如何实现处理线程的返回值

    技术2022-07-10  135

    如何给run()方法传参

    构造函数传参成员变量传参回调函数传参

    如何实现处理线程的返回值

    有时,程序的运行依赖子任务的返回值。当子任务交给子线程去完成的时候,需要获取他们的返回值,这就要考虑如何获取子线程的返回值。有三种解决方式

    1.主线程等待法。

    主线程循环等待,直到目标子线程返回值为止

    public class CycleWait implements Runnable { private String value; @Override public void run() { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } value = "we have data now"; } public static void main(String[] args) { CycleWait cw = new CycleWait(); Thread thread = new Thread(cw); thread.start(); System.out.println("value: "+cw.value); } }

    运行结果

    value: null

    main线程并没有等到子线程给value赋值就往下执行了,没有获取到value的值,接下来代码改造

    public class CycleWait implements Runnable { private String value; @Override public void run() { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } value = "we have data now"; } public static void main(String[] args) throws InterruptedException { CycleWait cw = new CycleWait(); Thread thread = new Thread(cw); thread.start(); while (cw.value == null){ Thread.sleep(100); } System.out.println("value: "+cw.value); } }

    打印结果

    value: we have data now

    通过判断cw的value值是否为null,如果为null,就一直将主线程休眠,知道子线程返回值。这就是主线程等待法。 优点 实现简单 缺点

    需要自己实现循环等待的逻辑。当等待的变量增加,while中要判断的变量会增加,会非常臃肿。等待的时间不确定,无法精准控制。比如等待中的100毫秒内有值,也无法将程序直接向下进行

    使用Thread类的join()方法阻塞当前线程以等待子线程处理完毕

    public class CycleWait implements Runnable { private String value; @Override public void run() { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } value = "we have data now"; } public static void main(String[] args) throws InterruptedException { CycleWait cw = new CycleWait(); Thread thread = new Thread(cw); thread.start(); // while (cw.value == null){ // Thread.sleep(100); // } thread.join(); System.out.println("value: "+cw.value); } }

    打印结果

    value: we have data now

    优点 实现简单,控制精准 缺点 粒度不够细。当主线程创建多个子线程时,判断某个线程返回值为某个值是,让另外子线程去run,这种更精准的依赖关系,join无法做到。

    通过Callable接口实现:通过FutureTask Or 线程池获取

    jdk5之前,线程是没有返回值的,想要获取返回值要大费周折

    FutureTask

    public class MyCallable implements Callable<String> { @Override public String call() throws Exception { String value = "test"; System.out.println("ready to work"); Thread.sleep(5000); System.out.println("task done"); return value; } } public class FutureTaskDemo { public static void main(String[] args) throws ExecutionException, InterruptedException { FutureTask<String> task = new FutureTask<>(new MyCallable()); new Thread(task).start(); if (!task.isDone()){ System.out.println("not finish"); } System.out.println("task return:" +task.get()); } }

    运行结果

    not finish ready to work task done task return:test

    FutureTask中有isDone()方法判断FutureTask构造方法传入的Callable实现类是否执行完成。FutureTask中get()方法会阻塞当前线程,直到FutureTask构造方法传入的Callable实现类的call()方法执行完毕,最后return返回值

    线程池

    public class ThreadPoolDemo { public static void main(String[] args) { ExecutorService executorService = Executors.newCachedThreadPool(); Future<String> future = executorService.submit(new MyCallable()); if (!future.isDone()){ System.out.println("not finish"); } try { System.out.println(future.get()); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } finally { executorService.shutdown(); } } }

    打印结果

    not finish ready to work task done test

    使用线程池,可以提交多个实现Callable的类,去让线程池并发的去处理结果。方便对实现Callable的类统一管理。

    Processed: 0.011, SQL: 9