import java.util.ArrayList;
import java.util.List;
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;
}
}
public void excute() 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<T> myThread = new MyThread(newList, begin, end) {
@Override
public void method(List list) {
callBack.method(list);
}
};
//執行線程
executorService.execute(myThread);
}
//計數器減一
begin.countDown();
end.await();
//關閉線程池
executorService.shutdown();
}
//抽象線程類
public abstract class MyThread<T> implements Runnable{
private List<T> list;
private CountDownLatch begin,end;
public MyThread(List<T> 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();
}finally {
//計數器減一
end.countDown();
}
}
public abstract void method(List<T> list);
}
//回調接口定義
public interface CallBack<T>{
public void method(List<T> list);
}
public static void main(String[] agrs){
List<String> list = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
list.add("hello"+i);
}
ThreadPoolTool<String> tool = new ThreadPoolTool(100,list);
tool.setCallBack(new CallBack<String>() {
@Override
public void method(List<String> list) {
for (int i = 0; i < list.size(); i++) {
System.out.print(Thread.currentThread().getId()+":"+list.get(i)+" ");
}
System.out.println();
}
});
try {
tool.excute();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}