//多线程处理
final List<PageData> dataList = Collections.synchronizedList(tables);
int dataSize = tables.size();
//拆分成多个任务,每个任务包含step条数据
int step = 10;
if (dataSize <= 10) {
step = dataSize;
} else if (dataSize <= 100 && dataSize > 10) {
step = dataSize / 5;
} else {
step = dataSize / 10;
}
int totalTasks = (dataSize % step == 0 ? dataSize / step : (dataSize / step + 1));
System.out.println(totalTasks);
final CountDownLatch countDownLatch = new CountDownLatch(totalTasks);
final CyclicBarrier cyclicBarrier = new CyclicBarrier(totalTasks);
Semaphore semaphore = new Semaphore(1);
long startTime1 = System.currentTimeMillis();
//线程数量对程序耗时有不同影响
// ExecutorService executorService = Executors.newFixedThreadPool(15);
ExecutorService executorService = Executors.newCachedThreadPool();
if (tables.size() > 0) {
try {
for (int j = 0; j < dataSize; j = j + step) {
final int start = j;
final int perCount = (dataSize - start) < step ? (dataSize - start) : step;
System.out.println(perCount);
executorService.execute(new Runnable() {
public void run() {
try {
semaphore.acquire();
List<PageData> list = Collections.synchronizedList(new ArrayList<>());
//业务逻辑...
list = Collections.synchronizedList(statisticalAnalysisService.getXX(page));
lists.addAll(list);
semaphore.release();
countDownLatch.countDown();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
countDownLatch.await(); // 回到主线程
//cyclicBarrier.await();
System.out.println("线程池循环耗时=======" + (System.currentTimeMillis() - startTime1));
executorService.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
在for循环中使用多线程丢失数据问题解决方案
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...