Java 多线程任务分解-ForkJoinPool(带返回值)示例

BaiduGetDishs.java类
 
  
package com.hwm.api.baidu.impl.dish;

import com.hwm.api.baidu.BaiduEnvironment;
import com.hwm.api.baidu.entity.dish.BaiduDish;
import com.hwm.api.baidu.entity.dish.BaiduMenu;
import com.hwm.api.baidu.impl.BaiduDishApi;
import com.hwm.exception.WMApiException;
import com.hwm.organization.entity.ApiResult;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.concurrent.ForkJoinPool;

public class BaiduGetDishs {

    private final static ForkJoinPool POOL = new ForkJoinPool();

    private BaiduGetDishs() {
    }

    /**
     用过任务分解获取
     @param baiduMenuList
     @param apiEnvironment
     @return
     */
    public static List getDishsFast(List baiduMenuList, BaiduEnvironment apiEnvironment) { //第一个参数是任务内容id,第二参数是任务环境
        if (CollectionUtils.isEmpty(baiduMenuList)) {
            return new ArrayList<>();
        }


        List baiduDishIds=new  ArrayList<>();
        for (int i=0;i> orderGroups;
        //最多只能分解6个任务
        //尽量平均分配
        if (baiduDishIds.size() <= 7) {
            orderGroups = dishIdDecompose(baiduDishIds, 1);
        } else if (baiduDishIds.size() <= 28) {
            orderGroups = dishIdDecompose(baiduDishIds, 2);
        } else if (baiduDishIds.size() <= 60) {
            orderGroups = dishIdDecompose(baiduDishIds, 3);
        } else if(baiduDishIds.size() <= 80){
            orderGroups = dishIdDecompose(baiduDishIds, 4);
        } else if(baiduDishIds.size() <= 120){
            orderGroups = dishIdDecompose(baiduDishIds, 5);
        }else{
            orderGroups = dishIdDecompose(baiduDishIds, 6);
        }

        BaiduDishApi baiduDishApi = new BaiduDishApi(apiEnvironment);
        DishRecursiveTask task = new DishRecursiveTask(orderGroups, baiduDishApi);
        POOL.submit(task);

        try {
            return task.get();
        }catch (Exception e) {
            throw new WMApiException(e);
        }

    }


    /**
     菜品分解
     @param baiduDishIds
     @param group 分解析的组数

     @return
     */
    private static Map> dishIdDecompose(List baiduDishIds, int group) {
        Map> groups = new HashMap<>();
        //分组不能小于1 大于 8
        if (group <= 0 || group >= 7) {
            throw new WMApiException("菜品分组不合理");
        }

        float flag = 1.0f * baiduDishIds.size() / group;  //平均下去
        int offset = Math.round(flag);

        Iterator iterator = baiduDishIds.iterator();
        for (int i = 1; i <= group; i++) {
            List item = new ArrayList<>();
            groups.put(i, item);
            for (int j = 1; j <= offset; j++) {
                if (iterator.hasNext()) {
                    item.add(iterator.next());
                } else {
                    break;
                }
            }
        }
        while (iterator.hasNext()) {
            groups.get(group).add(iterator.next());
        }
        return groups;
    }

}
DishRecursiveTask.java类
 
  
 
  
package com.hwm.api.baidu.impl.dish;

import com.hwm.api.baidu.entity.BaiduCategory;
import com.hwm.api.baidu.entity.dish.BaiduDish;
import com.hwm.api.baidu.entity.dish.BaiduDishList;
import com.hwm.api.baidu.entity.dish.BaiduSearch;
import com.hwm.api.baidu.impl.BaiduDishApi;
import com.hwm.exception.WMApiException;
import com.hwm.organization.entity.ApiResult;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.RecursiveTask;

public class DishRecursiveTask extends RecursiveTask> {


    private final BaiduDishApi baiduDishApi;
    private final Map> orderGroups;
    private final List dishIds;


    DishRecursiveTask(Map> orderGroups, BaiduDishApi baiduDishApi) {
        int groupNo = 0;
        if (orderGroups != null) {
            groupNo = orderGroups.size();
        }
        logger.debug("创建了一个分解任务,任务数:【{}】", groupNo);

        if (baiduDishApi == null) {
            throw new WMApiException("API百度创建GetDishRecursiveTask 失败");
        }
        this.orderGroups = orderGroups;
        this.baiduDishApi = baiduDishApi;
        this.dishIds = null;
    }

    private DishRecursiveTask(List dishIds, BaiduDishApi baiduDishApi) {
        int orderNumber = 0;
        if (dishIds != null) {
            orderNumber = dishIds.size();
        }
        logger.debug("创建了一个任务,订单数:【{}】", orderNumber);
        this.baiduDishApi = baiduDishApi;
        this.dishIds = dishIds;
        this.orderGroups = null;
    }


    @Override
    protected List compute() {

        List result = new ArrayList<>();

        //执行任务-获取菜品
        if (CollectionUtils.isNotEmpty(dishIds)) {
            for (HashMap dishId : dishIds) {
                //执行任务,将结果add
                result.add("");
            }

            return result;
        }

        if (orderGroups == null || orderGroups.isEmpty()) {
            return new ArrayList<>();
        }

        //创建任务分解
        List tasks = new ArrayList<>();
        for (Integer groupNo : orderGroups.keySet()) {
            List dishIds = orderGroups.get(groupNo);
            if (CollectionUtils.isEmpty(dishIds)) {
                continue;
            }
            DishRecursiveTask subTask = new DishRecursiveTask(dishIds, this.baiduDishApi);
            tasks.add(subTask);
        }

        //执行任务分解
        for (DishRecursiveTask task : tasks) {
            task.fork();
        }


        //执行任务合并
        for (DishRecursiveTask task : tasks) {
            try {
                List baiduOrders = task.join();
                if (CollectionUtils.isNotEmpty(baiduOrders)) {
                    result.addAll(baiduOrders);
                }
            }catch (Exception e) {
                throw new WMApiException(e);
            }
        }
        return result;
    }
}

 
  
 
  

大家只需要修改任务内容部分的代码和重新定义异常类。即可实现java任务的分解(带返回值)。

希望能帮到大家。大家如果有兴趣可以来我的网站看看:http://www.jiegelo.com

你可能感兴趣的:(随手笔记)