【Java】深夜代码祭(2)

问题

随便用用Future和Stream

描述

重感冒,考验受挫,还有还没到深夜

代码

import javafx.util.Pair;

import java.util.*;
import java.util.concurrent.*;
import java.util.stream.IntStream;

public class Main {

    private final static Random r = new Random(Calendar.getInstance().getTimeInMillis());

    private static ArrayList<DistributeDataSet> ddss;

    private static OriDataSet ods;

    private static void initialize() {

        ods = new OriDataSet().config(IntStream.rangeClosed(1, 1000000)
                .mapToObj((i) -> r.nextInt(1000))
                .collect(LinkedList<Integer>::new,
                        (LinkedList<Integer> l, Integer i) -> l.add(i),
                        (LinkedList<Integer> l1, LinkedList<Integer> l2) -> l1.addAll(l2)));

        ddss = new ArrayList<DistributeDataSet>();

        for(int i = 0; i < 5; ++i) ddss.add(new DistributeDataSet());
    }

    public static void main(String[] args) {

        initialize();

        ExecutorService e = Executors.newCachedThreadPool();

        ArrayList<FutureTask<Pair<LinkedList<Integer>, LinkedList<Integer>>>> fas = new ArrayList<>();

        for(int i = 0; i < 5; ++i) fas.add(getFutureTask(ods, i));

        fas.forEach(e::submit);

        fas.forEach((f) -> {

            try {

                Pair<LinkedList<Integer>, LinkedList<Integer>> p = f.get();

                System.out.println(p.getKey().size() + " " + p.getValue().size());
            }

            catch (InterruptedException | ExecutionException ex) {

                ex.printStackTrace();
            }
        });

        System.out.println("Done");

        e.shutdown();
    }

    private static FutureTask<Pair<LinkedList<Integer>, 
        LinkedList<Integer>>> getFutureTask(final OriDataSet ds, final int no) {

        return new FutureTask<Pair<LinkedList<Integer>, LinkedList<Integer>>>(() -> {

            while(ds.updateWithEffects(ddss.get(no))) { }

            return ddss.get(no).getResult();
        });
    }
}

class OriDataSet {

    private int cnt = 0;

    private LinkedList<Integer> data;

    public OriDataSet config(LinkedList<Integer> l) { data = l; return this; }

    public synchronized boolean updateWithEffects(DistributeDataSet dds) {

        if(!data.isEmpty() && dds.accept(data.getFirst()) && sideEffects());

        return !data.isEmpty();
    }

    private boolean sideEffects() {

        data.removeFirst();

        if((cnt += 1) % 1000 == 0) { System.out.println("now :" + cnt); }

        return true;
    }
}

class DistributeDataSet {

    protected int cnt;

    protected DistributeCondition<Integer> d1, d2;

    DistributeDataSet() {

        d1 = new EvenDistributeDataSet();

        d2 = new OddDistributeDataSet();
    }

    public Pair<LinkedList<Integer>, LinkedList<Integer>> getResult() {

        return new Pair<>(d1.getResult(), d2.getResult());
    }

    public boolean accept(Integer i) {

        return d1.accept(i) || d2.accept(i);
    }
}

abstract class DistributeCondition<T> {

    protected LinkedList<T> l;

    DistributeCondition() {

        l = new LinkedList<>();
    }

    abstract public boolean accept(T t);

    public LinkedList<T> getResult() {

        return l;
    }
}

class EvenDistributeDataSet extends DistributeCondition<Integer> {

    @Override
    public boolean accept(Integer t) {

        return t % 2 == 0 && sideEffect(t);
    }

    private boolean sideEffect(Integer t) {

        l.add(t);

        return true;
    }
}

class OddDistributeDataSet extends DistributeCondition<Integer> {

    @Override
    public boolean accept(Integer t) {

        return t % 2 != 0 && sideEffect(t);
    }

    private boolean sideEffect(Integer t) {

        l.add(t);

        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值