并行for循环

流式for循环

    private static void test1() {
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            arrayList.add(i + "");
        }
        arrayList.stream().forEach(e -> System.out.println(Thread.currentThread().getName() + " " + e));
    }

并行流式for循环

    private static void test2() {
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            arrayList.add(i + "");
        }
        arrayList.parallelStream().forEach(e -> System.out.println(Thread.currentThread().getName() + " " + e));
    }

forkjoin 并行流式for循环

    private static final ForkJoinPool pool = new ForkJoinPool(5);

    private static void test3() {
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            arrayList.add(i + "");
        }
        pool.submit(() -> arrayList.parallelStream().forEach(e -> {
            System.out.println(Thread.currentThread().getName() + " " + e);
        })).join();
    }

并行MAP

    private static void test4() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(100, 100, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(1));
        List<String> arrayList  =new ArrayList<>();
        for(int i = 0; i < 100; ++i){
            arrayList .add(i + "");
        }
        //开始前
        Map<String, String> resultMap = arrayList .parallelStream().collect(Collectors.toMap(t -> t, t -> {
            try {
                threadPoolExecutor.submit(() -> t).get();
            } catch (Exception e) {
                log.error("异常:{}" + e.getMessage());
            }
            return "";
        }));
    }

优化并行MAP

    private static void test5() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(100, 100, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(1));
        List<String> arrayList  =new ArrayList<>();
        for(int i = 0; i < 100; ++i){
            arrayList .add(i + "");
        }
        //优化后
        Map<String, Future<String>> futureMap = arrayList.stream().collect(Collectors.toMap(t -> t, t -> threadPoolExecutor.submit(() -> t),(oldValue, newValue)->newValue));
        futureMap.entrySet().stream().collect(Collectors.toMap(e->e.getKey(),e->{
            try {
                e.getValue().get();
            }catch (Exception t) {
                log.error("异常:{}" + t.getMessage());
            }
            return "";
        }));
    }

执行

    public static void main(String[] args) {
        test1();
        test2();
        test3();
        test4();
        test5();
    }

空处理方式

return Optional.ofNullable();

return Lists.newArrayList();

implements DependencyBase, Nullable{}

extends Nullable{}

return Collections.emptyList();

return empty();

return null;

return Optional.empty();

return new NullObject();

return new Object();

return BigDecimal.ZERO;
 
return Collections.emptyMap();

return Collections.emptySet();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值