zoukankan      html  css  js  c++  java
  • FUTURE .get 异常抛出会如何提示

    package com.dzhou.svntool.test;
    
    import java.util.concurrent.*;
    
    public class CallableFutureTest {
    
        public static void main(String[] args) throws InterruptedException, ExecutionException {
            System.out.println("start main thread");
            final ExecutorService exec = Executors.newFixedThreadPool(5);
    
            Callable<String> call = new Callable<String>() {
                public String call() {
    
                    System.out.println(" start new thread." + Thread.currentThread().getName());
                    try {
                        Thread.sleep(1000 * 5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName());
                    String name = Thread.currentThread().getName().substring(Thread.currentThread().getName().length()-1,Thread.currentThread().getName().length());
    
                    if (name.equals("3") ) {
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("come in"+name);
                        throw new RuntimeException("3");
                    }
                    if (name.equals("1") ) {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("come in"+name);
                        throw new RuntimeException("1");
                    }
                    if (name.equals("2") ) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("come in"+name);
                        throw new RuntimeException("2");
                    }
                    if (name.equals("4") ) {
                        try {
                        Thread.sleep(4000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(" end new thread." + Thread.currentThread().getName());
                    // call方法返回值
                    return "some value.";
                }
            };
            Future<String> task = exec.submit(call);
            Future<String> task1 = exec.submit(call);
            Future<String> task2 = exec.submit(call);
            Future<String> task3 = exec.submit(call);
            try {
                // 阻塞当前线程,即主线程,并等待子线程结束
                task.get();
                task1.get();
                task2.get();
                task3.get();
            } catch (Exception e) {
                System.out.println(" error-" + Thread.currentThread().getName() + e.getMessage());
            } finally {
                System.out.println(" finally .");
                exec.shutdown();
            }
            Thread.sleep(1000 * 2);
    
            System.out.println("end main thread");
        }
    }

    主线程捕捉的异常是第一个future.get的异常,不随时间发生先后变化

  • 相关阅读:
    大端与小端编号方法的区别
    socket、listen 等函数的打电话隐喻
    windows 网络编程报错 error LNK2019
    有符号数与无符号数之间的转换
    C++ 代码命名建议
    编写启发式代码的方法
    求给定数目的前 n 个素数
    不使用 “+” 实现加法操作
    二叉搜索树中两个节点的旋转
    Python玩转硬件:TPYBoard-Micropython开发板大盘点
  • 原文地址:https://www.cnblogs.com/dzhou/p/13203872.html
Copyright © 2011-2022 走看看