lianshufeng 1 anno fa
parent
commit
92c0f78845

+ 29 - 25
super/PCore/src/main/java/com/github/microservice/core/util/queue/ExecuteQueueUtil.java

@@ -41,7 +41,7 @@ public class ExecuteQueueUtil {
         sleepExecute(1, item);
         countDownLatch.await();
 
-        return item.getResult().getData();
+        return item.getResult() != null ? item.getResult().getData() : null;
     }
 
     // 延迟执行
@@ -53,29 +53,32 @@ public class ExecuteQueueUtil {
                     setStartTime(item.getStartTime());
                 }};
                 //记录结果
+                item.setResult(null);
                 item.setResult(item.getHandle().execute(runTime));
             } catch (Exception e) {
                 e.printStackTrace();
                 log.error(e.getMessage());
             }
 
-            // 异步则退出阻塞
-            if (item.getResult().isAsync()) {
-                countDown(item.getCountDownLatch());
-            }
 
-            // 完成则退出阻塞
-            if (item.getResult().isDone()) {
-                countDown(item.getCountDownLatch());
-                // 结束,不在执行循环
-                return;
+            if (item.getResult() != null) {
+                // 异步则退出阻塞
+                if (item.getResult().isAsync()) {
+                    countDown(item.getCountDownLatch());
+                }
+
+                // 完成则退出阻塞
+                if (item.getResult().isDone()) {
+                    countDown(item.getCountDownLatch());
+                    // 结束,不在执行循环
+                    return;
+                }
             }
 
             if (index < item.getMaxTryCount()) {
                 sleepExecute(index + 1, item);
             } else {
                 countDown(item.getCountDownLatch());
-                return;
             }
         }, item.getSleepTime().get(index), TimeUnit.MILLISECONDS);
     }
@@ -138,38 +141,39 @@ public class ExecuteQueueUtil {
         private long startTime;
     }
 
-    
+
 //    @SneakyThrows
 //    public static void main(String[] args) {
 //
-//        String a = execute(10, (index) -> {
-//            return Map.of(1, 1000L, 2, 1000L, 3, 1000L, 4, 5000L).getOrDefault(index, 5000L);
+//        String a = execute(6, (index) -> {
+//            return Map.of(1, 1000L, 2, 1000L, 3, 1000L, 4, 2000L).getOrDefault(index, 3000L);
 //        }, (runTime) -> {
 //            long time = System.currentTimeMillis() - runTime.getStartTime();
+//            var ret = new Result<String>();
 //            System.out.println("------------" + Thread.currentThread());
 //            System.out.println(runTime.getIndex() + "," + time);
 //
 //            //异常
-////            if (runTime.getIndex() >= 3) {
-////                throw new RuntimeException("模拟异常");
-////            }
+//            if (runTime.getIndex() >= 3) {
+//                throw new RuntimeException("模拟异常");
+//            }
 //
-////            if (runTime.getIndex() >= 5) {
+////            if (runTime.getIndex() >= 3) {
 ////                return new Result<String>().setData("放弃").setDone(true);
 ////            }
 //
 //            // 时间异步
-//            if (time > 3000) {
-//                return new Result<String>().setData("异步线程继续").setAsync(true);
-//            }
+////            if (time > 3000) {
+////                ret.setData("异步线程继续").setAsync(true);
+////            }
 //
 //            // 次数模拟完成
-////            if (runTime.getIndex() >= 5) {
-////                return new Result<String>().setData("完成").setDone(true);
-////            }
+//            if (runTime.getIndex() >= 3) {
+//                return new Result<String>().setData("完成").setDone(true);
+//            }
 //
 //
-//            return new Result<String>().setData("未完成");
+//            return ret;
 //        });
 //
 //