java中的fork-join框架

import com.google.common.collect.Lists;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.RecursiveTask;

/**
 * 批量插入数据任务类
 * 利用fork-join框架对数据插入任务进行分组,变成分组插入
 *
 * @author wangjing
 */
public class BatchInsertTask extends RecursiveTask<Integer> {
    /**
     * 要插入的数据
     */
    List<Integer> records;
    private static final Integer OFFSET = 5;

    public BatchInsertTask(List<Integer> records) {
        this.records = records;
    }

    @Override
    protected Integer compute() {
        //当要插入的数据少于3,则直接插入
        if (records.size() <= OFFSET) {
            return computeDirectly();
        } else {
            //如果要插入的数据大于等于3,则进行分组插入
            int size = records.size();
            int limit = size % OFFSET == 0 ? size / OFFSET : size / OFFSET + 1;
            Integer sum = 0;
            List<BatchInsertTask> taskList = Lists.newArrayList();
            for (int i = 0; i < limit; i++) {
                int max = OFFSET * (i + 1) > size ? size : OFFSET * (i + 1);
                BatchInsertTask task = new BatchInsertTask(records.subList(i * OFFSET, max));
                task.fork();
                taskList.add(task);
            }
            for (BatchInsertTask task : taskList) {
                sum += task.join();
            }
            return sum;
        }
    }

    /**
     * 真正插入数据的逻辑
     */
    private int computeDirectly() {
        try {
            Thread.sleep((long) (records.size() * 1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("插入了:" + Arrays.toString(records.toArray()));
        return records.size();
    }
}

  

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;

/**
 * 练习使用fork-join
 *
 * @author wangjing
 */
public class TestForkJoin {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ForkJoinPool forkJoinPool = new ForkJoinPool(8);
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(1);
        list.add(1);
        list.add(1);
        list.add(1);
        list.add(1);
        list.add(1);
        list.add(1);
        list.add(1);
        list.add(1);
        list.add(1);
        list.add(1);

        BatchInsertTask batchInsertTask = new BatchInsertTask(list);
        ForkJoinTask<Integer> reslut = forkJoinPool.submit(batchInsertTask);
        System.out.println(reslut.get());
     forkJoinPool.shutdown();
} }

  

posted @ 2018-12-18 20:55  奋小斗g  阅读(254)  评论(0编辑  收藏  举报