From bca4fcbd6b2cfe16f90fe01cc3a7b41491ab85c8 Mon Sep 17 00:00:00 2001 From: "USER-20220102CG\\noblelift" <546428999@qq.com> Date: Sun, 23 Apr 2023 16:43:35 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BB=BB=E5=8A=A1=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/InstructionServiceImpl.java | 44 +++++++++------- .../task/service/impl/TaskServiceImpl.java | 50 ++++++++++++------- .../org/nl/modules/system/util/CodeUtil.java | 34 +++++++------ 3 files changed, 77 insertions(+), 51 deletions(-) diff --git a/acs/hd/nladmin-system/src/main/java/org/nl/acs/instruction/service/impl/InstructionServiceImpl.java b/acs/hd/nladmin-system/src/main/java/org/nl/acs/instruction/service/impl/InstructionServiceImpl.java index c893728..796a770 100644 --- a/acs/hd/nladmin-system/src/main/java/org/nl/acs/instruction/service/impl/InstructionServiceImpl.java +++ b/acs/hd/nladmin-system/src/main/java/org/nl/acs/instruction/service/impl/InstructionServiceImpl.java @@ -51,6 +51,7 @@ import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.*; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.regex.Pattern; /** @@ -63,7 +64,7 @@ import java.util.regex.Pattern; @Slf4j public class InstructionServiceImpl implements InstructionService, ApplicationAutoInitial { - List instructions = new ArrayList(); + List instructions = new CopyOnWriteArrayList(); private final DeviceAppService deviceAppService; @@ -80,7 +81,8 @@ public class InstructionServiceImpl implements InstructionService, ApplicationAu @Override public synchronized void reload() { - this.instructions = this.queryAll("instruction_status <2 and is_delete =0"); + List list = this.queryAll("instruction_status <2 and is_delete =0"); + this.instructions = new CopyOnWriteArrayList<>(list); } @Override @@ -442,14 +444,16 @@ public class InstructionServiceImpl implements InstructionService, ApplicationAu JSONObject json = (JSONObject) JSONObject.toJSON(dto); wo.update(json); + removeByCodeFromCache(dto.getInstruction_code()); + +// Iterator iterator = instructions.iterator(); +// while (iterator.hasNext()) { +// Instruction instruction = iterator.next(); +// if (instruction.getInstruction_code().equals(dto.getInstruction_code())) { +// iterator.remove(); +// } +// } - Iterator iterator = instructions.iterator(); - while (iterator.hasNext()) { - Instruction instruction = iterator.next(); - if (instruction.getInstruction_code().equals(dto.getInstruction_code())) { - iterator.remove(); - } - } if (StrUtil.equals(dto.getInstruction_status(), "0") || StrUtil.equals(dto.getInstruction_status(), "1")) { instructions.add(dto); } @@ -1160,15 +1164,19 @@ public class InstructionServiceImpl implements InstructionService, ApplicationAu @Override public boolean removeByCodeFromCache(String code) { - - Iterator iterator = instructions.iterator(); - while (iterator.hasNext()) { - Instruction instruction = iterator.next(); - if (instruction.getInstruction_code().equals(code)) { - iterator.remove(); - return true; - } - } +// +// Iterator iterator = instructions.iterator(); +// while (iterator.hasNext()) { +// Instruction instruction = iterator.next(); +// if (instruction.getInstruction_code().equals(code)) { +// iterator.remove(); +// return true; +// } +// } + CopyOnWriteArrayList instructions = (CopyOnWriteArrayList) this.instructions; + instructions.removeIf((inst) -> { + return inst.getInstruction_code().equals(code); + }); return false; } diff --git a/acs/hd/nladmin-system/src/main/java/org/nl/acs/task/service/impl/TaskServiceImpl.java b/acs/hd/nladmin-system/src/main/java/org/nl/acs/task/service/impl/TaskServiceImpl.java index 7c3fa81..ef44596 100644 --- a/acs/hd/nladmin-system/src/main/java/org/nl/acs/task/service/impl/TaskServiceImpl.java +++ b/acs/hd/nladmin-system/src/main/java/org/nl/acs/task/service/impl/TaskServiceImpl.java @@ -56,6 +56,7 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.math.BigDecimal; import java.util.*; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -69,7 +70,7 @@ import java.util.regex.Pattern; @Slf4j public class TaskServiceImpl implements TaskService, ApplicationAutoInitial { - List tasks = new ArrayList(); + List tasks = new CopyOnWriteArrayList<>(); private final DeviceAppService deviceAppService; @@ -97,7 +98,8 @@ public class TaskServiceImpl implements TaskService, ApplicationAutoInitial { @Override public synchronized void reload() { - this.tasks = this.queryAll("task_status <2 and is_delete =0"); + List list = this.queryAll("task_status <2 and is_delete =0 order by create_time"); + tasks = new CopyOnWriteArrayList<>(list); } @Override @@ -455,7 +457,9 @@ public class TaskServiceImpl implements TaskService, ApplicationAutoInitial { JSONObject json = (JSONObject) JSONObject.toJSON(dto); wo.insert(json); - tasks.add(dto); + synchronized(TaskServiceImpl.class){ + tasks.add(dto); + } } /** @@ -590,12 +594,15 @@ public class TaskServiceImpl implements TaskService, ApplicationAutoInitial { wo.update(json); Iterator iterator = tasks.iterator(); - while (iterator.hasNext()) { - TaskDto task = iterator.next(); - if (task.getTask_code().equals(dto.getTask_code())) { - iterator.remove(); - } - } +// while (iterator.hasNext()) { +// TaskDto task = iterator.next(); +// if (task.getTask_code().equals(dto.getTask_code())) { +// iterator.remove(); +// } +// } + + removeByCodeFromCache(entity.getTask_code()); + if (StrUtil.equals(dto.getTask_status(), "0") || StrUtil.equals(dto.getTask_status(), "1")) { tasks.add(dto); } @@ -1031,15 +1038,22 @@ public class TaskServiceImpl implements TaskService, ApplicationAutoInitial { @Override public boolean removeByCodeFromCache(String code) { - Iterator iterator = tasks.iterator(); - while (iterator.hasNext()) { - TaskDto task = iterator.next(); - if (task.getTask_code().equals(code)) { - iterator.remove(); - return true; - } - } - return false; +// Iterator iterator = tasks.iterator(); +// while (iterator.hasNext()) { +// TaskDto task = iterator.next(); +// if (task.getTask_code().equals(code)) { +// iterator.remove(); +// return true; +// } +// } +// return false; + + CopyOnWriteArrayList taskDtos = (CopyOnWriteArrayList) this.tasks; + taskDtos.removeIf((task) -> { + return task.getTask_code().equals(code); + }); + return true; + } @Override diff --git a/acs/hd/nladmin-system/src/main/java/org/nl/modules/system/util/CodeUtil.java b/acs/hd/nladmin-system/src/main/java/org/nl/modules/system/util/CodeUtil.java index 340cf84..081ec7a 100644 --- a/acs/hd/nladmin-system/src/main/java/org/nl/modules/system/util/CodeUtil.java +++ b/acs/hd/nladmin-system/src/main/java/org/nl/modules/system/util/CodeUtil.java @@ -8,22 +8,26 @@ import java.util.List; public class CodeUtil { - public static synchronized String getNewCode(String ruleCode) { - GenCodeService service = new GenCodeServiceImpl(); - String flag = "1"; - HashMap map = new HashMap<>(); - map.put("flag", flag); - map.put("code", ruleCode); - return service.codeDemo(map); + public static String getNewCode(String ruleCode){ + synchronized (ruleCode) { + GenCodeService service=new GenCodeServiceImpl(); + String flag = "1"; + HashMap map = new HashMap<>(); + map.put("flag",flag); + map.put("code",ruleCode); + return service.codeDemo(map); + } } - public static synchronized List getNewCodes(String ruleCode, Integer number) { - GenCodeService service = new GenCodeServiceImpl(); - String flag = "1"; - HashMap map = new HashMap<>(); - map.put("flag", flag); - map.put("code", ruleCode); - map.put("number", number + ""); - return service.codeDemos(map); + public static List getNewCodes(String ruleCode, Integer number) { + synchronized (ruleCode) { + GenCodeService service = new GenCodeServiceImpl(); + String flag = "1"; + HashMap map = new HashMap<>(); + map.put("flag", flag); + map.put("code", ruleCode); + map.put("number", number + ""); + return service.codeDemos(map); + } } }