RecursiveTask操作

package com.my.utils;

import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.RecursiveTask;
import java.util.stream.Collectors;

public class ComputeTask extends RecursiveTask<double[]> {
    private Map<String, Double> dataList;//列表

    private Set<String> keys;
    private int limit = 100;//任务分割最小单位

    public ComputeTask(Map<String, Double> mps,Set<String> keys) {
        super();
        this.dataList = mps;
        this.keys = keys;
    }

    @Override
    protected double[] compute() {
        if (dataList.size() <= limit) {
            return doComputeThins(dataList,keys);//真正干活操作
        }

        // 将数据拆成两份
        int count_0 = dataList.size()/2;
        int count_1 = dataList.size() - count_0;

        Map<String, Double> t0 = takeOut(dataList, count_0);
        ComputeTask c0 = new ComputeTask(t0,keys);
        c0.fork();

        Map<String, Double> t1 = takeOut(dataList, count_1);
        ComputeTask c1 = new ComputeTask(t1,keys);
        c1.fork();

        //将两份数据合并
        double[] array = mergeArray(c0.join(),c1.join());

        return array;
    }

    /**
     * 拆数据操作
     * 取出Num条数据到另一个集合上
     * @param all
     * @param num
     * @return
     */
    private Map<String, Double> takeOut(Map<String, Double> all, int num) {
        Map<String, Double> r = new Hashtable<>();

        Set<Map.Entry<String, Double>> entries = all.entrySet();
        Iterator<Map.Entry<String, Double>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Double> next = iterator.next();
            String key = next.getKey();
            Double value = next.getValue();
            if (r.size() < num) {
                r.put(key, value);
                iterator.remove();
            }else {
                break;
            }
        }
        return r;
    }

    private double[] mergeArray(double[] a, double[] b) {
        int length = 100000;
        double[] r = new double[length];
        for (int i = 0; i < length; i++) {
            r[i] = a[i] + b[i];
        }
        a = null;
        b = null;
        return r;
    }


    /**
     * 此处开始干活操作
     * @param h
     * @return
     */
    public double[] doComputeThins(Map<String, Double> h,Set<String> keys) {
        double[] a = new double[100000];

        Set<Map.Entry<String, Double>> entries = h.entrySet();
        for (Map.Entry<String, Double> entry : entries) {
            String k = entry.getKey();
            Double v = entry.getValue();

            List<String> ks = keys.stream().filter(value -> value.contains(k)).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(ks)) {
                for (String kk : ks) {
                    //将key转换成数组下标,
                    kk = kk .replaceAll("w", "").replaceAll("q", "").replaceAll("b", "")
                            .replaceAll("s", "").replaceAll("g", "");
                    int index = Integer.parseInt(kk);
                    a[index] = a[index] + v;
                }
            }
        }

        h.clear();//释放内存

        return a;
    }
}

        RedisTemplate<String, String> tpl = new RedisConfig().redisTemplate();
        Set<String> keys = tpl.opsForSet().members("ss");

        ForkJoinPool pool = new ForkJoinPool();
        ForkJoinTask<double[]> task = new ComputeTask(mps,keys);

        ForkJoinTask<double[]> submit = pool.submit(task);

        try {
            double[] r = submit.get();
            minOrMax(r);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值