Bladeren bron

美术阅卷10月新增需求-随机数修改

wangliang 5 jaren geleden
bovenliggende
commit
e623c42786

+ 2 - 1
stmms-ms-admin/src/main/java/cn/com/qmth/stmms/ms/admin/service/DataUploadService.java

@@ -263,7 +263,8 @@ public class DataUploadService {
         int count = 0, result = 0;
         Long random = 0L;
         while (true) {
-            random = RandomUtil.randomMap.get(workId).get(new Random().nextInt(RandomUtil.randomMap.get(workId).size()));
+//            random = RandomUtil.randomMap.get(workId).get(new Random().nextInt(RandomUtil.randomMap.get(workId).size()));
+            random = RandomUtil.randomList.get(new Random().nextInt(RandomUtil.randomList.size()));
             result = paperRepo.countByWorkIdAndExamNumberAndRandomSeq(workId, examNumber, random);
             if (result == 0 && random != Long.parseLong(examNumber.substring(3, examNumber.length()))) {
                 break;

+ 24 - 25
stmms-ms-commons/src/main/java/cn/com/qmth/stmms/ms/commons/utils/RandomUtil.java

@@ -2,7 +2,10 @@ package cn.com.qmth.stmms.ms.commons.utils;
 
 import org.slf4j.LoggerFactory;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 
 /**
  * @Description: 随机数生成工具类
@@ -16,7 +19,8 @@ public class RandomUtil {
     public static final int minSize = 1000000;
     public static final int maxSize = 1000001;
     public static final int randomSize = 2000000;
-    public static Map<Long, List<Long>> randomMap = new HashMap<>();
+    //    public static Map<Long, List<Long>> randomMap = new HashMap<>();//用map感觉很占内存
+    public static List<Long> randomList = new ArrayList<>();//改用list
 
     /**
      * 根据workId随机生成百万随机数
@@ -25,29 +29,24 @@ public class RandomUtil {
      * @param againRandom
      */
     public static void getRandom(Long workId, boolean againRandom) {
-        List list = randomMap.get(workId);
-        if (Objects.isNull(list)) {
-            list = new ArrayList();
-        }
-        if (list.size() == 0 || againRandom) {
-            long start = System.currentTimeMillis();
-            Long randomReq = workId % 10;
-            randomReq = randomReq == 0L ? 1L : randomReq;
-            LOGGER.info("workId:{},开始生成随机数:{}", workId, start);
-            Long finalRandomReq = randomReq;
-            List finalList = list;
-            new Thread(() -> {
-                Set<Long> set = new HashSet<>();
-                for (int i = 0; i < randomSize; i++) {
-                    long id = (long) (Math.random() * minSize + maxSize * finalRandomReq);
-                    set.add(id);
-                }
-                finalList.addAll(set);
-                set.clear();
-                randomMap.put(workId, finalList);
-                long end = System.currentTimeMillis();
-                LOGGER.info("workId:{},生成随机数耗时:{},数据长度为:{}", workId, (end - start) / 1000 + "s", finalList.size());
-            }).start();
+        if (randomList.size() > 0) {
+            randomList.clear();
         }
+        long start = System.currentTimeMillis();
+        Long randomSeq = workId % 10;
+        randomSeq = randomSeq == 0L ? 1L : randomSeq;
+        LOGGER.info("workId:{},开始生成随机数:{}", workId, start);
+        Long finalRandomSeq = randomSeq;
+        new Thread(() -> {
+            Set<Long> set = new HashSet<>();
+            for (int i = 0; i < randomSize; i++) {
+                long id = (long) (Math.random() * minSize + maxSize * finalRandomSeq);
+                set.add(id);
+            }
+            randomList.addAll(set);
+            set.clear();
+            long end = System.currentTimeMillis();
+            LOGGER.info("workId:{},生成随机数耗时:{},数据长度为:{}", workId, (end - start) / 1000 + "s", randomList.size());
+        }).start();
     }
 }