Browse Source

ConcurrentHashMap,替换HashMap

malijun 8 months ago
parent
commit
62615cae32
1 changed files with 69 additions and 69 deletions
  1. 69 69
      ruoyi-admin/src/test/java/com/ruoyi/ZhiBiaoCalculation.java

+ 69 - 69
ruoyi-admin/src/test/java/com/ruoyi/ZhiBiaoCalculation.java

@@ -16,6 +16,7 @@ import org.springframework.test.context.junit4.SpringRunner;
 
 import javax.annotation.Resource;
 import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.atomic.AtomicReference;
 
 /**
@@ -58,17 +59,17 @@ public class ZhiBiaoCalculation {
         List<PointInfo> pointInfos = pointInfo.selectList(queryWrapper);
 
         //定义切入风速map集合
-        HashMap<String, HashMap<String, Double>> mapIn = new HashMap<>();
+        ConcurrentHashMap<String, ConcurrentHashMap<String, Double>> mapIn = new ConcurrentHashMap<>();
 
         //定义切出风速集合
-        HashMap<String, HashMap<String, Double>> mapOut = new HashMap<>();
+        ConcurrentHashMap<String, ConcurrentHashMap<String, Double>> mapOut = new ConcurrentHashMap<>();
 
 
         //遍历每台风机,取出每台风机的pointDatas
         for (PointInfo turbine : turbineZt) {
 
-            mapIn.put(turbine.getTurbineId(), new HashMap<>();
-            mapOut.put(turbine.getTurbineId(), new HashMap<>();
+            mapIn.put(turbine.getTurbineId(), new ConcurrentHashMap<>());
+            mapOut.put(turbine.getTurbineId(), new ConcurrentHashMap<>());
 
             List<PointData> pointDatas = turbine.getPointDatas();
 
@@ -76,8 +77,6 @@ public class ZhiBiaoCalculation {
             String windSpeedKey = null;
 
 
-
-
             //遍历pointInfos,找出pointData1的id和pointInfos中相等的,取出pointInfo的pointKey
             for (PointInfo info : pointInfos) {
                 if (Objects.equals(turbine.getTurbineId(), info.getTurbineId())) {
@@ -130,83 +129,84 @@ public class ZhiBiaoCalculation {
                     //存入map集合,外层key为turbineId,内层key为ts2,value为平均切出风速
 
                     mapOut.get(turbine.getTurbineId()).put(String.valueOf(ts2), avgCutOutWindSpeed);
-            }
-            //遍历map集合,取出每个风机的所有切入风速,算一个平均值
-            HashMap<String, Double> mapTurbineValues = mapIn.get(turbine.getTurbineId());
-
-            //平均切入风速
-            double avgCutInWindSpeed = 0;
-            if (mapTurbineValues != null) {
-                AtomicReference<Double> sum = new AtomicReference<>(0.0);
-                //遍历mapTurbineValues,V大于5的舍弃,剩余算平均值
-                for ( String key : mapTurbineValues.keySet()) {
-                    if (mapTurbineValues.get(key) > 5) {
-                        mapTurbineValues.remove(key);
-                    }
                 }
-                mapTurbineValues.forEach((k1, v) -> {
-                    sum.updateAndGet(v1 -> v1 + v);
-                });
-
-                //如果值为空则置0
-                if (sum.get() == 0) {
-                    avgCutInWindSpeed = 0;
-                } else {
-                    avgCutInWindSpeed= sum.get() / mapTurbineValues.size();
+                //遍历map集合,取出每个风机的所有切入风速,算一个平均值
+                ConcurrentHashMap<String, Double> mapTurbineValues = mapIn.get(turbine.getTurbineId());
+
+                //平均切入风速
+                double avgCutInWindSpeed = 0;
+                if (mapTurbineValues != null) {
+                    AtomicReference<Double> sum = new AtomicReference<>(0.0);
+                    //遍历mapTurbineValues,V大于5的舍弃,剩余算平均值
+                    for (String key : mapTurbineValues.keySet()) {
+                        if (mapTurbineValues.get(key) > 5) {
+                            mapTurbineValues.remove(key);
+                        }
+                    }
+                    mapTurbineValues.forEach((k1, v) -> {
+                        sum.updateAndGet(v1 -> v1 + v);
+                    });
+
+                    //如果值为空则置0
+                    if (sum.get() == 0) {
+                        avgCutInWindSpeed = 0;
+                    } else {
+                        avgCutInWindSpeed = sum.get() / mapTurbineValues.size();
+                    }
+                    System.out.println(turbine.getTurbineId() + "切入平均风速" + avgCutInWindSpeed + "时间" + timeBegin);
                 }
-                System.out.println(turbine.getTurbineId() + "切入平均风速" + avgCutInWindSpeed + "时间" + timeBegin);
-            }
 
-            //遍历map集合,取出每个风机的所有切出风速,算一个平均值
-            HashMap<String, Double> mapTurbineValues2 = mapOut.get(turbine.getTurbineId());
+                //遍历map集合,取出每个风机的所有切出风速,算一个平均值
+                ConcurrentHashMap<String, Double> mapTurbineValues2 = mapOut.get(turbine.getTurbineId());
 
-            double avgCutOutWindSpeed;
-            if (mapTurbineValues2 != null) {
-                AtomicReference<Double> sum = new AtomicReference<>(0.0);
+                double avgCutOutWindSpeed;
+                if (mapTurbineValues2 != null) {
+                    AtomicReference<Double> sum = new AtomicReference<>(0.0);
 
-                for ( String key : mapTurbineValues2.keySet()) {
-                    if (mapTurbineValues2.get(key) > 5) {
-                        mapTurbineValues2.remove(key);
+                    for (String key : mapTurbineValues2.keySet()) {
+                        if (mapTurbineValues2.get(key) > 5) {
+                            mapTurbineValues2.remove(key);
+                        }
                     }
+                    mapTurbineValues2.forEach((k2, v) -> {
+                        sum.updateAndGet(v1 -> v1 + v);
+                    });
+
+                    if (sum.get() == 0) {
+                        avgCutOutWindSpeed = 0;
+                    } else {
+                        avgCutOutWindSpeed = sum.get() / mapTurbineValues2.size();
+                    }
+                    System.out.println(turbine.getTurbineId() + "切出平均风速" + avgCutOutWindSpeed + "时间" + timeBegin);
                 }
-                mapTurbineValues2.forEach((k2, v) -> {
-                    sum.updateAndGet(v1 -> v1 + v);
-                });
-
-                if (sum.get() == 0) {
-                    avgCutOutWindSpeed = 0;
-                } else {
-                    avgCutOutWindSpeed = sum.get() / mapTurbineValues2.size();
-                }
-                System.out.println(turbine.getTurbineId() + "切出平均风速" + avgCutOutWindSpeed + "时间" + timeBegin);
-            }
 
-            //存入数据库
-            String turbineId = turbine.getTurbineId();
-            Date jdkDate = timeBegin.toJdkDate();
-            System.out.println(turbineId + " " + jdkDate);
-            QueryWrapper<TurbineInfoDay> turbineInfoDayQueryWrapper = new QueryWrapper<>();
-            turbineInfoDayQueryWrapper.eq("turbine_id", turbine.getTurbineId());
-            turbineInfoDayQueryWrapper.eq("record_date", timeBegin.toJdkDate());
-            TurbineInfoDay one = turbineInfoDayService.getOne(turbineInfoDayQueryWrapper);
-            System.out.println(one);
-            if (one == null) {
-                TurbineInfoDay turbineInfoDay = new TurbineInfoDay();
-                turbineInfoDay.setTurbineId(turbine.getTurbineId());
-                turbineInfoDay.setRecordDate(timeBegin.toJdkDate());
-                turbineInfoDay.setXfqrfs(avgCutInWindSpeed);
-                System.out.println(turbineInfoDay);
-//                turbineInfoDayService.save(turbineInfoDay);
-            }else {
-                one.setXfqrfs(avgCutInWindSpeed);
+                //存入数据库
+                String turbineId = turbine.getTurbineId();
+                Date jdkDate = timeBegin.toJdkDate();
+                System.out.println(turbineId + " " + jdkDate);
+                QueryWrapper<TurbineInfoDay> turbineInfoDayQueryWrapper = new QueryWrapper<>();
+                turbineInfoDayQueryWrapper.eq("turbine_id", turbine.getTurbineId());
+                turbineInfoDayQueryWrapper.eq("record_date", timeBegin.toJdkDate());
+                TurbineInfoDay one = turbineInfoDayService.getOne(turbineInfoDayQueryWrapper);
                 System.out.println(one);
+                if (one == null) {
+                    TurbineInfoDay turbineInfoDay = new TurbineInfoDay();
+                    turbineInfoDay.setTurbineId(turbine.getTurbineId());
+                    turbineInfoDay.setRecordDate(timeBegin.toJdkDate());
+                    turbineInfoDay.setXfqrfs(avgCutInWindSpeed);
+                    System.out.println(turbineInfoDay);
+//                turbineInfoDayService.save(turbineInfoDay);
+                } else {
+                    one.setXfqrfs(avgCutInWindSpeed);
+                    System.out.println(one);
 //                turbineInfoDayService.updateById(one);
-            }
+                }
 
 //            turbineInfoDayService.saveOrUpdate(one);
 
+            }
         }
-    }
 
 
+    }
 }