在for循环中使用多线程丢失数据问题解决方案

2019-09-20  本文已影响0人  一曲无忆
//多线程处理
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();
    }
}
上一篇下一篇

猜你喜欢

热点阅读