场景:有一个需求:首先从数据库中取出上千条数据,然后需要逐条对数据进行分析,例如分析情感/是否重复等多项分析。
解决:将取出来的上千条,分批+多线程处理。
public class CountDownLatchTest {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < 1000; i++) {
list.add(String.valueOf(i));
}
// int processors = Runtime.getRuntime().availableProcessors();
ExecutorService threadPool = Executors.newFixedThreadPool(10);
final CountDownLatch latch = new CountDownLatch(10);
List<List<String>> splitList = averageAssign(list, 10);
for (int i = 0; i < splitList.size(); i++) {
final List<String> temp = splitList.get(i);
threadPool.execute(() -> {
Runnable runnable = new Runnable() {
@Override
public void run() {
//这里可能有坑,要注意 countDown()一定要执行完 !!!
for (int j = 0; j < temp.size(); j++) {
System.out.println("内部for执行");
}
System.out.println("子线程" + Thread.currentThread().getName() + "执行完成"+temp);
latch.countDown();//当前线程调用此方法,则计数减一
}
};
threadPool.execute(runnable);
});
}
try {
latch.await();//阻塞当前线程,直到计数器的值为0
System.out.println("10 end ");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
threadPool.shutdown();
}
}
/**
* 将一组数据平均分成n组
*
* @param source 要分组的数据源
* @param n 平均分成n组
* @param <T>
* @return
*/
public static <T> List<List<T>> averageAssign(List<T> source, int n) {
List<List<T>> result = new ArrayList<List<T>>();
int remainder = source.size() % n; //(先计算出余数)
int number = source.size() / n; //然后是商
int offset = 0;//偏移量
for (int i = 0; i < n; i++) {
List<T> value = null;
if (remainder > 0) {
value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
remainder--;
offset++;
} else {
value = source.subList(i * number + offset, (i + 1) * number + offset);
}
result.add(value);
}
return result;
}
/**
* 将集合按len数量分成若干个list
*
* @param list
* @param len 每个集合的数量
* @return
*/
public static List<List<String>> splitList(List<String> list, int len) {
if (list == null || list.size() == 0 || len < 1) {
return null;
}
List<List<String>> result = new ArrayList<List<String>>();
int size = list.size();
int count = (size + len - 1) / len;
for (int i = 0; i < count; i++) {
List<String> subList = list.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
result.add(subList);
}
return result;
}
}