当前位置:   article > 正文

多线程与并发 - 如何实现处理线程的返回值_多线程调用如何实现返回异常

多线程调用如何实现返回异常

如何给run()方法传参

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

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

1、主线程等待法(即让主线程循环等待,直到目标子线程返回值为止)

  1. public class Cyclewait implements Runnable {
  2. private String value;
  3. @Override
  4. public void run() {
  5. try {
  6. Thread.sleep(5000);
  7. } catch (InterruptedException e) {
  8. e.printStackTrace();
  9. }
  10. value = "we have data now";
  11. }
  12. public static void main(String[] args) throws InterruptedException {
  13. Cyclewait cyclewait = new Cyclewait();
  14. Thread thread = new Thread(cyclewait);
  15. thread.start();
  16. // 主线程等待法,核心代码:当value为空时循环等待
  17. while (null == cyclewait.value) {
  18. Thread.sleep(100);
  19. }
  20. System.out.println("value:" + cyclewait.value);
  21. }
  22. }

运行结果:

value:we have data now

缺点:需要自己实现循环等待的逻辑,当需要等待的变量很多时,代码会显得异常臃肿;同时需要循环多久是未知的,无法做到更精准的控制

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

  1. public class Cyclewait implements Runnable {
  2. private String value;
  3. @Override
  4. public void run() {
  5. try {
  6. Thread.sleep(5000);
  7. } catch (InterruptedException e) {
  8. e.printStackTrace();
  9. }
  10. value = "we have data now";
  11. }
  12. public static void main(String[] args) throws InterruptedException {
  13. Cyclewait cyclewait = new Cyclewait();
  14. Thread thread = new Thread(cyclewait);
  15. thread.start();
  16. // 使用Thread类的join()阻塞当前线程以等待子线程处理完毕
  17. thread.join();
  18. System.out.println("value:" + cyclewait.value);
  19. }
  20. }

运行结果:

value:we have data now

Thread类的join()方法会阻塞调用此方法的线程,即这里会阻塞主线程,直到join()方法所在的线程执行完毕为止

缺点:无法处理更精准的依赖关系

3、通过Callable接口实现:通过FutureTask 或者 线程池获取

实现Callable接口

  1. import java.util.concurrent.Callable;
  2. public class MyCallable implements Callable<String> {
  3. @Override
  4. public String call() throws Exception {
  5. String value = "test";
  6. System.out.println("Ready to work");
  7. Thread.sleep(5000);
  8. System.out.println("task done");
  9. return value;
  10. }
  11. }
  • 通过FutureTask获取
  1. import java.util.concurrent.ExecutionException;
  2. import java.util.concurrent.FutureTask;
  3. public class FutureTaskDemo {
  4. public static void main(String[] args) throws ExecutionException, InterruptedException {
  5. FutureTask<String> futureTask=new FutureTask<String>(new MyCallable());
  6. new Thread(futureTask).start();
  7. if(!futureTask.isDone()){
  8. System.out.println("task has not finished,please wait!");
  9. }
  10. System.out.println("task return:"+futureTask.get());
  11. }
  12. }

运行结果:

  1. task has not finished,please wait!
  2. Ready to work
  3. task done
  4. task return:test
  • 通过线程池获取
  1. import java.util.concurrent.ExecutionException;
  2. import java.util.concurrent.ExecutorService;
  3. import java.util.concurrent.Executors;
  4. import java.util.concurrent.Future;
  5. public class ThreadPoolDemo {
  6. public static void main(String[] args) {
  7. ExecutorService executorService = Executors.newCachedThreadPool();
  8. Future<String> future = executorService.submit(new MyCallable());
  9. if (!future.isDone()) {
  10. System.out.println("task has not finished,please wait!");
  11. }
  12. try {
  13. System.out.println("task return:" + future.get());
  14. } catch (InterruptedException e) {
  15. e.printStackTrace();
  16. } catch (ExecutionException e) {
  17. e.printStackTrace();
  18. } finally {
  19. executorService.shutdown();
  20. }
  21. }
  22. }

运行结果:

  1. task has not finished,please wait!
  2. Ready to work
  3. task done
  4. task return:test

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/330247
推荐阅读
相关标签
  

闽ICP备14008679号