From da89067c48bbeba3cc4f8daddd30565455b61a8b Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Sat, 27 Sep 2025 01:07:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor(task):=E4=BC=98=E5=8C=96=E8=A7=86?= =?UTF-8?q?=E9=A2=91=E7=89=87=E6=AE=B5=E8=8E=B7=E5=8F=96=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E7=9A=84=E8=AE=BE=E5=A4=87=E8=AE=A1=E6=95=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 currentUnFinPlaceholder从 List 类型改为 Map- 使用 AtomicInteger 跟踪每个设备的未完成任务数量 - 在设备任务完成时正确减少计数并清理已完成的设备 - 更新进度日志以反映去重后的设备总数 --- .../com/ycwl/basic/task/VideoPieceGetter.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/ycwl/basic/task/VideoPieceGetter.java b/src/main/java/com/ycwl/basic/task/VideoPieceGetter.java index 2fd4da1a..c1e5c102 100644 --- a/src/main/java/com/ycwl/basic/task/VideoPieceGetter.java +++ b/src/main/java/com/ycwl/basic/task/VideoPieceGetter.java @@ -53,6 +53,7 @@ import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -145,7 +146,7 @@ public class VideoPieceGetter { new ArrayBlockingQueue<>(128), threadFactory ); - List currentUnFinPlaceholder = new ArrayList<>(); + Map currentUnFinPlaceholder = new ConcurrentHashMap<>(); List list = faceSampleMapper.listByIds(task.getFaceSampleIds()); Map pairDeviceMap = new ConcurrentHashMap<>(); if (!list.isEmpty()) { @@ -169,12 +170,12 @@ public class VideoPieceGetter { }) .collect(Collectors.groupingBy(FaceSampleEntity::getDeviceId)); if (templatePlaceholder != null) { - IntStream.range(0, templatePlaceholder.size()).forEach(i -> { - currentUnFinPlaceholder.add(templatePlaceholder.get(i)); + templatePlaceholder.forEach(deviceId -> { + currentUnFinPlaceholder.computeIfAbsent(deviceId, k -> new AtomicInteger(0)).incrementAndGet(); }); } else { - collection.keySet().forEach(i -> { - currentUnFinPlaceholder.add(i.toString()); + collection.keySet().forEach(deviceId -> { + currentUnFinPlaceholder.put(deviceId.toString(), new AtomicInteger(1)); }); } collection.values().forEach(faceSampleList -> { @@ -197,14 +198,21 @@ public class VideoPieceGetter { log.info("找到同景区关联设备:{} -> {}", pairDeviceId, faceSample.getDeviceId()); if (pairDeviceId != null) { doCut(pairDeviceId, faceSample.getId(), faceSample.getCreateAt(), task); - currentUnFinPlaceholder.remove(faceSample.getDeviceId().toString()); + AtomicInteger count = currentUnFinPlaceholder.get(faceSample.getDeviceId().toString()); + if (count != null && count.decrementAndGet() <= 0) { + currentUnFinPlaceholder.remove(faceSample.getDeviceId().toString()); + } } }); } doCut(faceSample.getDeviceId(), faceSample.getId(), faceSample.getCreateAt(), task); - currentUnFinPlaceholder.remove(faceSample.getDeviceId().toString()); + AtomicInteger count = currentUnFinPlaceholder.get(faceSample.getDeviceId().toString()); + if (count != null && count.decrementAndGet() <= 0) { + currentUnFinPlaceholder.remove(faceSample.getDeviceId().toString()); + } if (templatePlaceholder != null) { - log.info("当前进度:!{}/{}", currentUnFinPlaceholder.size(), templatePlaceholder.size()); + long distinctDeviceCount = templatePlaceholder.stream().distinct().count(); + log.info("当前进度:!{}/{}", currentUnFinPlaceholder.size(), distinctDeviceCount); if (currentUnFinPlaceholder.isEmpty()) { if (!invoke.get()) { invoke.set(true);