zoukankan      html  css  js  c++  java
  • ForkJoinPoolet用于类似递归

    package cn.temp.temp;
    
    import java.util.concurrent.ForkJoinPool;
    import java.util.concurrent.RecursiveTask;
    
    // F:myeclipseWorker	empincn	emp	emp
    // java -classpath .; cn.temp.temp.Temp
    // java -classpath . cn.temp.temp.Temp
    public class Temp {
    
        public static void main(String[] args) throws Exception {
            long start;
            int count = 20;
            
            start = System.currentTimeMillis();
            int value = a(count);
            System.out.println(value);
            System.out.println("用时:" + (System.currentTimeMillis() - start) + " ms");
    
            start = System.currentTimeMillis();
            ForkJoinPool pool = new ForkJoinPool();
            Task task = new Task(count);
            pool.invoke(task);
            System.out.println(task.getRawResult());
            System.out.println("用时:" + (System.currentTimeMillis() - start) + " ms");
        }
    
        public static int a(int j) {
            if (j <= 2)
                return 1;
            return a(j - 2) + a(j - 1);
        }
    }
    
    @SuppressWarnings("serial")
    class Task extends RecursiveTask<Integer> {
        int    i;
    
        Task(int i) {
            super();
            this.i = i;
        }
    
        @Override
        protected Integer compute() {
            if (i <= 2)
                return 1;
            Task t1 = new Task(i - 1);
            Task t2 = new Task(i - 2);
            invokeAll(t1, t2);
            return t1.getRawResult() + t2.getRawResult();
        }
    
    }
  • 相关阅读:
    2171 棋盘覆盖
    [网络流24题] 骑士共存
    COGS28 [NOI2006] 最大获利[最大权闭合子图]
    1066: [SCOI2007]蜥蜴
    1877: [SDOI2009]晨跑
    POJ 2125 Destroying the Graph 二分图最小点权覆盖
    LA 3231
    3028: 食物
    PYOJ 44. 【HNSDFZ2016 #6】可持久化线段树
    1597: [Usaco2008 Mar]土地购买
  • 原文地址:https://www.cnblogs.com/feng2015/p/4511567.html
Copyright © 2011-2022 走看看