@Test
public void testByCity() throws InterruptedException {
// 1. 获取所有医院数据
List<TrhHospitalList> allHospitals = trhHospitalListMapper.selectList(new LambdaQueryWrapper<TrhHospitalList>()
.eq(TrhHospitalList::getHospitalCity, "")
);
// 2. 配置线程池(根据API的QPS限制调整线程数)
int maxConcurrentThreads = 5;
ExecutorService executor = Executors.newFixedThreadPool(maxConcurrentThreads);
// 3. 计数器
AtomicInteger successCount = new AtomicInteger(0);
AtomicInteger processedCount = new AtomicInteger(0);
int total = allHospitals.size();
// 4. 使用CountDownLatch等待所有任务完成
CountDownLatch latch = new CountDownLatch(total);
// 5. 提交任务(每个医院一个独立任务,线程池自动调度)
for (TrhHospitalList hospital : allHospitals) {
executor.submit(() -> {
try {
if (hospital.getHospitalCity().equals("") && hospital.getHospitalCounty().equals("")) {
boolean success = updateHospitalLocation(hospital);
if (success) {
successCount.incrementAndGet();
}
}
} catch (Exception e) {
System.err.println("处理失败,ID: " + hospital.getId() + ", 错误: " + e.getMessage());
} finally {
latch.countDown();
int progress = processedCount.incrementAndGet();
if (progress % 1000 == 0) {
System.out.printf("进度: %d/%d (%.1f%%)%n",
progress, total, (progress * 100.0 / total));
}
}
});
}
// 6. 等待所有任务完成
latch.await();
executor.shutdown();
System.out.println("更新完成!成功数量: " + successCount.get());
}