参考1:
https://wenku.baidu.com/view/a9cdf1c09889680203d8ce2f0066f5335a81672a.html
参考2:
https://www.cnblogs.com/weilx/p/16329743.html
1.一条简单粗暴的路:
使用java的parallelStream可以轻松实现并发,不需要考虑多线程的问题,但有一些要注意的地方
a.使用 parallelStream可能存在的bug
在parallelStream的外部创建List 或者Map,然后在parallelStream内部对List或者Map进行塞值得时候,可能会因为抢占资源,导致部分元素丢失。
b. 如何正确使用 parallelStream
一个简单的方法是使用java的收集器Collectors配合parallelStream的collect函数。
2.另一条路
另一条路是再Spring中自定义线程池,然后配合并发调用
1.定义线程池
import com.dianping.cat.Cat;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.ymm.framework.dgc.thread.executor.EnhancedThreadPoolExecutor; //可以使用java原生线程池
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Configuration
@EnableAsync
public class Thread {
@Bean(name = "cargoServiceExecutor")
public EnhancedThreadPoolExecutor cargoServiceExecutor() {
EnhancedThreadPoolExecutor executor =
new EnhancedThreadPoolExecutor((3 * Runtime.getRuntime().availableProcessors()) + 1, 60, 120L, TimeUnit.SECONDS,
new LinkedBlockingQueue(1024));
executor.setRejectedExecutionHandler(new CustomKafkaDiscardPolity());
executor.setThreadFactory(new ThreadFactoryBuilder().setNameFormat("cargoService-thread-%d").build());
return executor;
}
static class CustomKafkaDiscardPolity extends ThreadPoolExecutor.DiscardPolicy {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
Cat.logEvent("ThreadPoolDisCard", "cargoService");
}
}
}
2.stream配合异步CompletableFuture,使用线程池
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
List<CargoDto> cargoDtoList = Lists.newArrayList();
List<CompletableFuture<List<CargoDto>>> routeFeatureDTOResFutureList = new ArrayList<>();
Lists.partition(cargoIdList, realtimeCargoLion.getQueryCargoBatch()).stream().forEach(list -> {
CompletableFuture<List<CargoDto>> routeFeatureDTOResFuture = CompletableFuture.supplyAsync(() -> rpcService.queryByIds(list),cargoServiceExecutor);
routeFeatureDTOResFutureList.add(routeFeatureDTOResFuture);
});
routeFeatureDTOResFutureList.forEach(future -> {
try {
List<CargoDto> routeFeatureDTOResList = future.get(realtimeCargoLion.getQueryCargoBatchTimeout(), TimeUnit.MILLISECONDS);
cargoDtoList.addAll(routeFeatureDTOResList);
} catch (Exception e) {
}
});