Friday, August 14, 2020

The note about Java 8 Parallel Streams vs ExecutorService

 The Java 8 parallel streams look neat, compact and easy, however, it has limitations you cannot specify the thread number or thread pool.

I did a comparison using the two implementations, I have a list of stock code, and need access the URL to get the name for the code list (3470 code).

For serial retrieval, it needs 900+ seconds.

For parallel streams retrieval, it spends 138 seconds.

For ExecutorService, it spends 45 seconds for 20 threads, 18 seconds for 50 threads, and 5.75 seconds for 200 threads.

//from 900+ seconds to 138 seconds for parallelStream
List<Code> codeList = codeStringList.parallelStream().map( (code)->{
String name = null;
try {
name = SinaDataProvider.getName(code);
} catch (IOException e) {
logger.error("Fatal error to get name for "+code+","+e.getMessage());
e.printStackTrace();
}
logger.debug(code+","+name);
Code codeObj = new Code();
codeObj.setCode(code);
//remove blank space from name
codeObj.setName(name.replaceAll("\\s",""));
codeObj.setStart_time(yyyyMMdd);
codeObj.setEnd_time("");
return codeObj;
}).collect(Collectors.toList());
//45 seconds for 20 threads for ExecutorService 
//18 seconds for 50 threads
//5.75 seconds for 200 threads
ExecutorService executor = Executors.newFixedThreadPool(200);
Collection<Future<Code>> futures = new LinkedList<Future<Code>>();
for(String code:codeStringList){
futures.add(
executor.submit(()->{
String name = null;
try {
name = SinaDataProvider.getName(code);
} catch (IOException e) {
logger.error("Fatal error to get name for "+code+","+e.getMessage());
e.printStackTrace();
}
logger.debug(code+","+name);
Code codeObj = new Code();
codeObj.setCode(code);
//remove blank space from name
codeObj.setName(name.replaceAll("\\s",""));
codeObj.setStart_time(yyyyMMdd);
codeObj.setEnd_time("");
return codeObj;
}));
};
List<Code> codeList = new ArrayList<>();
for (Future<Code> future : futures) {
try {
Code codeObj = future.get();
codeList.add(codeObj);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
executor.shutdown();

No comments:

Post a Comment