无线程池实现
while(存在未对账订单){// 查询未对账订单Thread T1 = new Thread(()->{pos = getPOrders();});T1.start();// 查询派送单Thread T2 = new Thread(()->{dos = getDOrders();});T2.start();// 等待 T1、T2 结束T1.join();T2.join();// 执行对账操作diff = check(pos, dos);// 差异写入差异库save(diff);
}
线程池+CountDownLatch实现
// 创建 2 个线程的线程池
Executor executor = Executors.newFixedThreadPool(2);
while(存在未对账订单){// 计数器初始化为 2CountDownLatch latch = new CountDownLatch(2);// 查询未对账订单executor.execute(()-> {pos = getPOrders();latch.countDown();});// 查询派送单executor.execute(()-> {dos = getDOrders();latch.countDown();});// 等待两个查询操作结束latch.await();// 执行对账操作diff = check(pos, dos);// 差异写入差异库save(diff);
}