问题
随便用用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;
}
}