【Java】深夜代码祭(2)
2016-02-16 19:11
302 查看
问题
随便用用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; } }
相关文章推荐
- java中常用算法—二分法查找
- Java中关于时区的哪些事
- JAVA进行基础的文件IO读写
- Java注解@Retention&@Inherited@Target@IntDef@Documented
- 初试spark java WordCount
- SpringMVC解决GET请求时中文乱码的问题
- struts2的jsp读取action参数
- Java api 入门教程 之 JAVA的文件操作
- 启动eclipse闪退,无法启动eclipse
- MyEclipse设置Java代码注释模板
- Quartz在Spring中动态设置cronExpression (spring设置动态定时任务)
- STS 断点有斜线不起作用的解决方法
- SpringMVC 处理客户端请求的过程
- Spring
- 关于struts2 的单例和多例及线程安全的问题
- java正则表达式特殊字符
- Java程序打印日历
- struts2 action获取jsp参数的三种方式
- 记录-java执行请求的URL
- Java中转UTC时间字符串(含有T Z)为local时间