场景:有一个需求:首先从数据库中取出上千条数据,然后需要逐条对数据进行分析,例如分析情感/是否重复等多项分析。
解决:将取出来的上千条,分批+多线程处理。
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 = splitList(list, 100);
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() {
try {
System.out.println("子线程" + Thread.currentThread().getName() + "执行完成"+temp);
} finally {
latch.countDown();//当前线程调用此方法,则计数减一
}
}
};
threadPool.execute(runnable);
});
}
try {
latch.await();//阻塞当前线程,直到计数器的值为0
System.out.println("10 end ");
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
threadPool.shutdown();
}
}
/**
* 将集合按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;
}
}
|