Java并发的四种风味:Thread、Executor、ForkJoin和Actor

Java并发的四种风味:Thread、Executor、ForkJoin和Actor 
ForkJoin工具类与实现demo 
1.工具类

package thread.demo_027;

import java.util.concurrent.RecursiveTask;


public class ForkJoinWork extends RecursiveTask<Long>{

    private Long start;//开始值

    private Long end;//介绍值

    public static final Long critical =100000L;//临界值

    public ForkJoinWork(Long start, Long end) {
        this.start=start;
        this.end=end;
    }

    @Override
    protected Long compute() {
        //判断是否拆分
        Long lenth=end-start;
        if(lenth<=critical){
            //如果拆分完毕就相加
            Long sum=0L;
            for (Long i = start; i <=end; i++) {
                sum+=i;
            }
          return sum;

        }else{
            //没有拆分完毕就开始拆分
            Long middle=(end+start)/2;//计算2个值得中间值

            ForkJoinWork right=new ForkJoinWork(start,middle);

            right.fork();//拆分,并压入线程队列

            ForkJoinWork left=new ForkJoinWork(middle+1, end);

            left.fork(); //拆分 并加入线程队列
            //合并
            return right.join()+left.join();
        }


    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

2.工具类实现

package thread.demo_027;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.RecursiveTask;

/**
 * Fork/Join框架demo
 *
 */
public class ForkJoinDemo {

    private static  int THRESLOD=10;//任务阈值

    /**
     * 求1到100万之间的和,因为需要返回结果,所以需要继承recursiverTask
     */
  static class  MC extends RecursiveTask<Long>{

        Long sum = Long.valueOf(0);

        private  int begin,end;

        public MC(int begin, int end){
            this.begin=begin;
            this.end=end;
        }

        //表示这个任务完成后,返回的一个值
        @Override
        protected Long compute() {

            //如果任务量小于阈值,就直接计算
            if ((end-begin)<=THRESLOD){
                for (int i = begin; i <end ; i++) {
                    sum +=i;
                }
            }else{//如果大于1000, 就把他拆分成两个子任务进行fork

                int mid= (end+begin)/2;

                MC left=new MC(begin,mid);//一部分小线程
                left.fork();//开启这小部分线程

                MC right= new MC(mid,end);

                right.fork();


                Long li= left.join();//让left任务完整完成

                Long lr= right.join();//让right任务完整完成

                sum=li+lr;

            }
            return sum;
        }
    }


    public static void main(String[] args) throws  Exception{

        ForkJoinPool forkJoinPool =new ForkJoinPool();//创建他的线程池

        Future<Long> ft=forkJoinPool.submit(new MC(0,10));//在线程池中进行计算

        System.out.println("计算的结果是:"+ft.get());

        forkJoinPool.shutdown();//关闭线程池
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

3.原理、ForkJoin框架实现: 
就是将并发的线程进行拆分、然后进行合并、但是有一个难题、比如一个复杂逻辑的 
怎么将一个线程任务、拆分多个任务、保证数据完整性与一致性。就相当于synchronized的不可见性与原子性。 
这里写图片描述

posted @ 2018-08-01 16:33  绅士的野兽  阅读(281)  评论(0)    收藏  举报