Collections.synchronizedList ╰半橙微兮° 2022-10-23 12:43 13阅读 0赞 ArrayList是非线程安全的,在多线程的情况下,向list插入数据的时候,可能会造成数据丢失的情况.并且一个线程在遍历List,另一个线程修改List,会报ConcurrentModificationException(并发修改异常)错误. Vector是一个线程安全的List,但是它的线程安全实现方式是对所有操作都加上了synchronized关键字,这种方式严重影响效率.所以并不推荐使用Vector. Java 中 ArrayList 和 LinkedList 都不是线程安全的,但可以通过 java.util.Collections.synchronizedList(List list) 方法,获取一个线程安全的 List 实例对象。 if(listA!=null&&listA.size()>0) { List<AcctBalanceEntity> list140000=Collections.synchronizedList(new ArrayList<>()); CompletableFuture[] cfsA = listA.stream() .map(i -> CompletableFuture .supplyAsync(() -> { List<AcctBalanceEntity> listTmp=new ArrayList<>(); ProdInstAcctRelBean bean=new ProdInstAcctRelBean(); List<ProdInstAcctRelBean> beanList=new ArrayList<>(); bean.setAcctId(i.getAcctId()); bean.setRouteId(i.getAcctId()); ErrorInfo errorInfo = dataInfoQueryInter.queryProdInstAcctRelList(bean, beanList); for (ProdInstAcctRelBean prodInstAcctRelBean : beanList) { ProdInstBean prodInstBean=new ProdInstBean(); List<ProdInstBean> prodInstBeanList=new ArrayList<>(); prodInstBean.setProdInstId(prodInstAcctRelBean.getProdInstId()); dataInfoQueryInter.queryProdInstList(prodInstBean,prodInstBeanList); if(prodInstBeanList!=null&&prodInstBeanList.size()>0) { Optional<ProdInstBean> op = prodInstBeanList.stream().filter(e->e.getStatusCd().equals("140000")).findFirst(); if(op.isPresent()) { listTmp.add(i); } } } return listTmp;}, poolExecutor) .thenApply(h -> h).whenComplete((s, e) -> { list140000.addAll(s); })).toArray(CompletableFuture[]::new); CompletableFuture.allOf(cfsA).join(); listA.removeAll(list140000); }
还没有评论,来说两句吧...