java线程为什么这样没有输出
黄舟
黄舟 2017-04-18 10:49:08
0
3
517
 List<String> list = Arrays.asList("a","b","c","d");
 ExecutorService service = Executors.newFixedThreadPool(2);
 service.execute(() -> list.parallelStream().forEach(System.out::println));

这样一段代码什么都没输出,而将parallelStream去掉就可以输出,这是什么机制?谁能解释下

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(3)
阿神

The parallelStream method is executed concurrently, which is equivalent to turning on the thread to output sout.
The reason why there is no output is that the main thread has finished executing, and the sub-thread has finished. There is no soout at this time. Add a wait at the end of the code and you can see the effect

黄舟

parallel...What is this method used for?

巴扎黑

The answer on the second floor is very good
It is equivalent to opening n+ threads instead of n

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!