shichongfu
2023-04-25 ce0b49552668d3331055e2b1a1447a743dc54939
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.product.org.admin.util;
 
import com.alibaba.fastjson.JSONArray;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class ThreadPoolTool<T> {
 
    //单个线程处理的数据量
    private int singleCount;
    //处理的总数据量
    private int listSize;
    //开启的线程数
    private int runSize;
    //操作的数据集
    private List<T> list;
    //计数器
    private CountDownLatch begin, end;
    //线程池
    private ExecutorService executorService;
    //回调
    private CallBack callBack;
 
    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }
 
    public  ThreadPoolTool(int singleCount, List<T> list) {
        this.singleCount = singleCount;
        this.list = list;
        if (list != null) {
            this.listSize = list.size();
            this.runSize = (this.listSize / this.singleCount) + 1;
            if(singleCount==1){
                this.runSize-=1;
            }
        }
    }
 
    public void excute(boolean async) throws InterruptedException {
        executorService = Executors.newFixedThreadPool(runSize);
        begin = new CountDownLatch(1);
        end = new CountDownLatch(runSize);
        //创建线程
        int startIndex = 0;
        int endIndex = 0;
        List<T> newList = null;
        for (int i = 0; i < runSize; i++) {
            //计算每个线程对应的数据
            if (i < (runSize - 1)) {
                startIndex = i * singleCount;
                endIndex = (i + 1) * singleCount;
                newList = list.subList(startIndex, endIndex);
            } else {
                startIndex = i * singleCount;
                endIndex = listSize;
                newList = list.subList(startIndex, endIndex);
            }
            //创建线程类处理数据
            MyThread myThread = new MyThread(newList, begin, end) {
                @Override
                public void method(List list) {
                    callBack.method(list);
                }
            };
            //执行线程
            if (!async) {
                executorService.submit(myThread);
            } else {
                executorService.execute(myThread);
            }
        }
        //计数器减一
        begin.countDown();
        end.await();
        //关闭线程池
        executorService.shutdown();
    }
 
    //抽象线程类
    public abstract class MyThread<Object> implements Runnable {
 
        private List<Object> list;
        private CountDownLatch begin, end;
 
        public MyThread(List<Object> list, CountDownLatch begin, CountDownLatch end) {
            this.list = list;
            this.begin = begin;
            this.end = end;
        }
 
        @Override
        public void run() {
            try {
                //执行程序
                method(list);
                begin.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
                throw new  RuntimeException(e.getMessage());
            } finally {
                //计数器减一
                end.countDown();
            }
        }
 
        public abstract void method(List<Object> list);
    }
 
    //回调接口定义
    public interface CallBack<Object> {
        public void method(List<java.lang.Object> list);
    }
 
    public static void main(String[] args) {
        JSONArray array =new JSONArray();
        for (int i = 0; i < 5; i++) {
            Map a=new HashMap();
            int grade=(int)a.get("org_evel_parent");
            while(array.size()-1!=grade){
                array.add(new JSONArray());
            }
            array.getJSONArray(grade).add(a);
        }
    }
}