为什么多线程中使用 Future 获取不到异常? - V2EX
cirton
V2EX    Java

为什么多线程中使用 Future 获取不到异常?

  •  
  •   cirton Mar 11, 2019 3051 views
    This topic created in 2619 days ago, the information mentioned may be changed or developed.

    java 多线程, 程序执行时,想使用 Future 特性捕获异常,做进一步的处理。运行程序时发生了异常,但并没有捕获到异常。

    相关代码结构如下:

    ExecutorService executor = Executors.newFixedThreadPool(3); Future<Object> future = null; for (int i = 0; i < 3; i++) { future = executor.submit(new ThreadDemo()); try { future.get(); } catch (InterruptedException e) { System.out.println(String.format("handle exception in child thread. %s", e)); } catch (ExecutionException e) { System.out.println(String.format("handle exception in child thread. %s", e)); } } ///// call 方法实现 @Override public String call() throws Exception { try { client.runMethod(ip); } catch (Exception e) { System.out.println(String.format("handle exception in child thread. %s", e)); } return ""; } 

    在服务器端执行程序时报错,但并未捕获到异常。

    java.rmi.ConnectException: Connection refused to host: 172.17.0.6; nested exception is: java.net.ConnectException: Connection refused (Connection refused) at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619) at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216) at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202) at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:338) at sun.rmi.registry.RegistryImpl_Stub.lookup(RegistryImpl_Stub.java:112) at java.rmi.Naming.lookup(Naming.java:101) at com.net.client.DataNodeRemoteClient.runRemoteMethod(DataNodeRemoteClient.java:27) at com.net.client.DataNodeRemoteClientCallableThread$runTheThread.call(DataNodeRemoteClientCallableThread.java:84) at com.net.client.DataNodeRemoteClientCallableThread$runTheThread.call(DataNodeRemoteClientCallableThread.java:1) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: java.net.ConnectException: Connection refused (Connection refused) 

    是少了哪些步骤或者哪里操作有问题?

    Supplement 1    Mar 12, 2019
    原因已找到,在 call 方法中调用的方法已经做了异常处理。
    No Comments Yet
    About     Help     Advertise     Blog     API     FAQ     Solana     3272 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 32ms UTC 14:12 PVG 22:12 LAX 07:12 JFK 10:12
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86