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的異常,不隨時間發生先后變化


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM