Fork/Join模式

为了提高大量任务的执行效率,一般会使用线程池(ThreadPoolExecutor)。但是考虑到某些单任务本身就可以继续拆分并发执行(例如对1000W个数据进行排序可以使用分治思想),如何更好地提高这些任务的并发效率将成为一个问题。

 

Fork/Join模式解决了任务更小粒度拆分的问题,我们可以将一个任务拆分成多个可并发执行的部分,最后再对各个子任务的结果进行汇总,得到最终执行结果。

 

1.Fork/Join模式下的线程池:ForkJoinPool

ForkJoinPool中的每个worker线程都有自己的workQueue(而ThreadPoolExecutor是所有worker线程都从1个公共的工作队列中取任务)。当某个worker线程的队列任务都完成后,这个空闲的worker线程会从其它线程的工作队列中偷一个任务执行(也称work-stealing),以提高并发效率。

 

1.1 ForkJoinPool的实例化

ForkJoinPool commonPool = ForkJoinPool.commonPool();//jdk8提供了一个静态公共池,可以减少不必要的线程池。

public static ForkJoinPool forkJoinPool = new ForkJoinPool(8);//自己创建线程池

 

2.任务的封装:ForkJoinTask

ForkJoinTask代表在ForkJoinPool中运行的任务。

 

2.1 ForkJoinTask的子类

  • RecursiveAction    一个递归无结果的ForkJoinTask(没有返回值)
  • RecursiveTask    一个递归有结果的ForkJoinTask(有返回值)

 

2.2 fork方法

public final ForkJoinTask<V> fork() {

    Thread t;

    if((t = Thread.currentThread()) instanceof ForkJoinWorkerThread)

        ((ForkJoinWorkerThread) t).workQueue.push(this);

    else

        ForkJoinPool.common.externalPush(this);

    return this;

}

fork方法是把当前的ForkJoinTask(即子任务)交由ForkJoinPool异步执行。

若当前线程是ForkJoinWorkerThread,则ForkJoinTask会被自动交由所在的ForkJoinPool执行;否则交由静态ForkJoinPool执行。所以只要指定了第一个父任务所在的ForkJoinPool,就可以确保其子任务调用fork方法后也一定在这个ForkJoinPool中被执行。所以第一个任务必须指定ForkJoinPool:

//最大并发数为4

ForkJoinPool forkJoinPool = new ForkJoinPool(4);

//执行第一个task,(一个RecursiveAction或RecursiveTask的实现类,或者Runnable)

forkJoinPool.submit(task);

//输出结果

System.out.println(task.join());

 

2.3 invoke方法

开始执行任务,如果必要,等待计算完成(阻塞等待)。

 

2.4 invokeAll方法

    public static void invokeAll(ForkJoinTask<?>... tasks) {

        Throwable ex = null;

        int last = tasks.length - 1;

        //将tasks.length-1交由其它worker线程处理,1个task交由当前worker线程处理(这样利用当前线程提高了执行效率)

        for (int i = last; i >= 0; --i) {

            ForkJoinTask<?> t = tasks[i];

            if (t == null) {

                if (ex == null)

                    ex = new NullPointerException();

            }

            else if (i != 0)

                t.fork();

            else if (t.doInvoke() < NORMAL && ex == null)

                ex = t.getException();

        }

        for (int i = 1; i <= last; ++i) {

            ForkJoinTask<?> t = tasks[i];

            if (t != null) {

                if (ex != null)

                    t.cancel(false);

                else if (t.doJoin() < NORMAL)

                    ex = t.getException();

            }

        }

        if (ex != null)

            rethrow(ex);

    }

从源码可以看出,利用invokeAll方法执行多个子任务可以有效利用当前worker线程,避免线程的过多创建。

 

2.5 join方法

返回任务的执行结果(阻塞等待)

 

2.6 举例(一个compute方法的实现):

protected Long compute() {

    if (任务足够小?) {

        return computeDirect();

    }

    // 任务太大,一分为二:

    SumTask subtask1 = new SumTask(...);

    SumTask subtask2 = new SumTask(...);

    // 通过invokeAll方法执行所有子任务

    invokeAll(subtask1, subtask2);

    // 合并结果:

    Long subresult1 = subtask1.join();

    Long subresult2 = subtask2.join();

    return subresult1 + subresult2;

}

 

3.ForkJoinTask测试

import java.util.concurrent.ForkJoinPool;

import java.util.concurrent.RecursiveTask;

 

/**

 * ForkJoin任务的测试示例

 */

public class ForkJoinDemo {

    /**

     * 模拟求和任务:根据数组长度进行对应的线程睡眠

     */

    public static class AccTask extends RecursiveTask<Boolean> {

        private final int start;

        private final int end;

        private final int threshold;

 

        public AccTask(int start, int end, int threshold) {

            this.start = start;

            this.end = end;

            this.threshold = threshold;

        }

 

        @Override

        protected Boolean compute() {

            if (this.end - this.start <= this.threshold) {

                try {

                    Thread.sleep(this.end - this.start);

                } catch (InterruptedException i) {

                    i.printStackTrace();

                }

                return true;

            }

            int mid = (this.start + this.end) / 2;

            AccTask subTask1 = new AccTask(this.start, mid, this.threshold);

            AccTask subTask2 = new AccTask(mid, this.end, this.threshold);

            invokeAll(subTask1, subTask2);

            return true;

        }

    }

 

    public static void main(String[] args) {

        int length = 10000;

        int cpuCores = Runtime.getRuntime().availableProcessors();

        int threshold = length / (2 * cpuCores);

        System.out.printf("cpu核数:%d,数组长度:%d\n", cpuCores, length);

        AccTask accTask = new AccTask(0, length, threshold);

        long start = System.currentTimeMillis();

        ForkJoinPool.commonPool().submit(accTask);

        accTask.join();

        long end = System.currentTimeMillis();

        System.out.printf("Fork/Join模拟累加计算结束,耗时%dms\n", end - start);

        start = System.currentTimeMillis();

        try {

            Thread.sleep(length);

        } catch (InterruptedException i) {

            i.printStackTrace();

        }

        end = System.currentTimeMillis();

        System.out.printf("单线程模拟累加计算结束,耗时%dms\n", end - start);

    }

}

输出结果:

cpu核数:4,数组长度:10000

Fork/Join模拟累加计算结束,耗时3771ms

单线程模拟累加计算结束,耗时10002ms

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值