我正在尝试了解java 8并行流.我写了下面的代码,首先使用Executor,然后使用并行流.看起来平行流需要两倍(10秒)的时间与Executor接近(5秒).在我看来,并行流也应该表现出类似的性能.任何想法为什么并行流需要两倍的时间?我的电脑有8个核心.
/**
*
*/
package com.shashank.java8.parallel_stream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* @author pooja
*
*/
public class Sample {
public static int processUrl(String url) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Running Thread " + Thread.currentThread());
return url.length();
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
usingExecutor();
usingParallelStream(); …Run Code Online (Sandbox Code Playgroud)