多线程下载材料与主线程同步
业务场景:接收三方资源,用户实时查看下载
1、自定义线程池
private final LinkedBlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>(2048);
@Bean
public ExecutorService executorService (){
ExecutorService executor =
new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2,
Runtime.getRuntime().availableProcessors() * 4,
10, TimeUnit.SECONDS, blockingQueue,
ThreadPoolManager.namedThreadFactory("threadpool_name"),
new ThreadPoolExecutor.CallerRunsPolicy()
);
retturn executor;
}
2、下载材料方法
//下载文件
public void downloadFile(List fileList) {
CountDownLatch latch = new CountDownLatch(fileList.size());
AtomicInteger failNum = new AtomicInteger(0);
fileList.forEach(pojo->{
executorService.execute(()->{
//上传文件
uploadFile(pojo,latch,failNum);
});
});
try {
//8秒没有完成,自动取消
latch.await(8,TimeUnit.SECONDS);
}catch (Exception e){
//do exception handling
}
if(sbjl.get() != 0){
//business processing after failure
}
}
3、上传材料
public void uploadFile(Pojo pojo,CountDownLatch latch,AtomicInteger failNum){
InputStream intstream = null;
try {
URL url = new URL(pojo.getUrl());
HttpURLConnection huc = (HttpURLConnection)url.openConnection();
huc.setRequestMethod("GET");
huc.setDoOutput(true);
huc.setDoInput(true);
huc.setUseCaches(false);
huc.setConnectTimeout(2000);
huc.setReadTimeout(7000);
intstream = huc.getInputStream();
UploadRestul uploadRestul = FileUtis.upload(intstream,fileName);
pojo.setUrl(uploadRestul.getUrl());
}catch (Exception e){
failNum.incrementAndGet();
}finally {
latch.countDown();
if(!Objects.isNull(intstream)){
try {
intstream.close();
}catch (IOException e){
log.info("下载材料关闭流失败");
}
}
}
}