java Future 不能及时结束

wxmimperio
  • 720

如下程序运行结束后,还在主线程卡一阵才退出?

public static class Task implements Callable<String> {
        @Override
        public String call() throws Exception {
            String tid = String.valueOf(Thread.currentThread().getId());
            System.out.printf("Thread#%s : in call\n", tid);
            return tid;
        }
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        List<Future<String>> results = new ArrayList<Future<String>>();
        ExecutorService es = Executors.newCachedThreadPool();
        for(int i=0; i<100;i++)
            results.add(es.submit(new Task()));

        for(Future<String> res : results)
            System.out.println(res.get());
    }
Thread#12 : in call
Thread#109 : in call
Thread#110 : in call
Thread#105 : in call
Thread#104 : in call
Thread#88 : in call
Thread#106 : in call
Thread#107 : in call
Thread#103 : in call
Thread#101 : in call
Thread#102 : in call
Thread#99 : in call
Thread#97 : in call
Thread#96 : in call
Thread#98 : in call
Thread#94 : in call
Thread#111 : in call
Thread#95 : in call
Thread#89 : in call
Thread#108 : in call
Thread#91 : in call
Thread#100 : in call
Thread#92 : in call
Thread#93 : in call
Thread#90 : in call
Thread#87 : in call
Thread#86 : in call
Thread#85 : in call
Thread#84 : in call
Thread#83 : in call
Thread#82 : in call
Thread#81 : in call
Thread#80 : in call
Thread#79 : in call
Thread#78 : in call
Thread#77 : in call
Thread#76 : in call
Thread#75 : in call
Thread#74 : in call
Thread#73 : in call
Thread#72 : in call
Thread#71 : in call
Thread#70 : in call
Thread#69 : in call
Thread#68 : in call
Thread#67 : in call
Thread#66 : in call
Thread#65 : in call
Thread#64 : in call
Thread#63 : in call
Thread#62 : in call
Thread#61 : in call
Thread#60 : in call
Thread#59 : in call
Thread#58 : in call
Thread#57 : in call
Thread#56 : in call
Thread#55 : in call
Thread#54 : in call
Thread#53 : in call
Thread#52 : in call
Thread#49 : in call
Thread#48 : in call
Thread#51 : in call
Thread#50 : in call
Thread#47 : in call
Thread#46 : in call
Thread#45 : in call
Thread#44 : in call
Thread#43 : in call
Thread#42 : in call
Thread#41 : in call
Thread#40 : in call
Thread#39 : in call
Thread#38 : in call
Thread#37 : in call
Thread#36 : in call
Thread#35 : in call
Thread#34 : in call
Thread#33 : in call
Thread#32 : in call
Thread#31 : in call
Thread#30 : in call
Thread#29 : in call
Thread#28 : in call
Thread#27 : in call
Thread#26 : in call
Thread#25 : in call
Thread#24 : in call
Thread#23 : in call
Thread#22 : in call
Thread#21 : in call
Thread#20 : in call
Thread#19 : in call
Thread#18 : in call
Thread#17 : in call
Thread#16 : in call
Thread#15 : in call
Thread#14 : in call
Thread#13 : in call
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

实际上Future.get都拿到结果了

回复
阅读 2.9k
4 个回答

因为CachedThreadPool在的默认线程是有空闲时间,好像是60s吧 只有在60s内继续没有任务过来才会退出

加上es.shutdown()试试

ExecutorService 没有关闭,调用shutdown 方法,和 Future 没有关系

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏