ForkJoinTask使用设计

BatchForkTask
package com.shby.core.common;

import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;

/**
 * @author: 李小龙
 * @description:JAVA开发
 * @created:2019/9/6
 * @modified by 李小龙 on 2019/9/6
 */
public class BatchForkTask<T> extends RecursiveTask<T> {

    List objectList;

    ForkProcess process;

    private static final int THRESHOLD = 3;

    public BatchForkTask(List objectList, ForkProcess process) {
        this.objectList = objectList;
        this.process = process;
    }

    @Override
    protected T compute() {
        if (objectList.size() < THRESHOLD) {
           return process.process(objectList);
        } else {
            //如果要插入的数据大于等于3,则进行分组插入
            int size = objectList.size();
            //第一个分组
            BatchForkTask<T> aTask = new BatchForkTask(objectList.subList(0, size / 2), process);
            //第二个分组
            BatchForkTask<T> bTask = new BatchForkTask(objectList.subList(size / 2, objectList.size()), process);
            //两个任务并发执行起来
            invokeAll(aTask, bTask);
            //两个分组的插入的行数加起来
            return process.combine(aTask.join(), bTask.join());
        }
    }

    public static<D> D invokeFork(List objects, ForkProcess processer, int threadCount) {
        ForkJoinPool forkJoinPool = new ForkJoinPool(threadCount);
        BatchForkTask<D> batchForkTask = new BatchForkTask(objects, processer);
        ForkJoinTask<D> reslut = forkJoinPool.submit(batchForkTask);
        D serials = null;
        try {
            serials = reslut.get();
        } catch(Exception e) {
            e.printStackTrace();
        }
        return serials;
    }
}

 

ForkProcess
package com.shby.core.common;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 李小龙
 * @description:JAVA开发
 * @created:2019/9/6
 * @modified by 李小龙 on 2019/9/6
 */
public interface ForkProcess {

    <T> T process(List objectList);

    /**
     * 默认实现
     * @param obj
     * @param obj2
     * @param <T>
     * @return
     */
    default <T> T combine(T obj, T obj2) {
        if(obj instanceof List) {
            List list = new ArrayList();
            list.addAll((List)obj);
            list.addAll((List)obj2);
            return (T) list;
        } else {
            StringBuffer buffer = new StringBuffer();
            buffer.append(obj);
            buffer.append(obj2);
            return (T) buffer.toString();
        }
    }
}

Test测试

    @Test
    public void forkJoin() {
//        ForkJoinPool commonPool = ForkJoinPool.commonPool();
        List<ImsStockInOrderSerialDTO> imsStockInOrderSerialDTOList = new ArrayList<ImsStockInOrderSerialDTO>();
        for(int i=0; i<100000; i++) {
            ImsStockInOrderSerialDTO imsStockInOrderSerialDTO = new ImsStockInOrderSerialDTO();
            imsStockInOrderSerialDTO.setSerialNo("ER1234499292929" + i);
            imsStockInOrderSerialDTO.setSerialStatus(StatusFlag.SF_DEFAULT.CODE);
            imsStockInOrderSerialDTOList.add(imsStockInOrderSerialDTO);
        }
        String serials = BatchForkTask.invokeFork(imsStockInOrderSerialDTOList,  new ForkProcess(){
            @Override
            public String process(List objectList) {
                StringBuffer buffer = new StringBuffer();
                for(Object dto : objectList) {
                    ImsStockInOrderSerialDTO sdto = (ImsStockInOrderSerialDTO) dto;
                    buffer.append(sdto.getSerialNo());
                }
                return buffer.toString();
            }
        }, 10);
        System.out.println(serials);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值