Browse Source

增加异步业务执行器

johnclot69 4 months ago
parent
commit
a8c68bc9e4
1 changed files with 26 additions and 0 deletions
  1. 26 0
      incubator-game/src/main/java/com/incubator/game/GGame.java

+ 26 - 0
incubator-game/src/main/java/com/incubator/game/GGame.java

@@ -34,6 +34,7 @@ import org.slf4j.Logger;
 
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executor;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 
@@ -97,6 +98,9 @@ public class GGame extends AbstractService {
     /** 本服所有在线玩家 */
     public static Map<String, GPlayer> onlinePlayers = new ConcurrentHashMap<>();
 
+    /** 异步业务执行器 */
+    protected Executor asyncExec;
+
     public static GlobalInfoPO globalInfoPO = new GlobalInfoPO();
 
     private static volatile GGame instance;
@@ -122,6 +126,28 @@ public class GGame extends AbstractService {
         return lsDBHandler;
     }
 
+    public void asyncExec(final Runnable command) {
+        if (command == null) {
+            logger.error("asyncExec command is null!");
+            return;
+        }
+        asyncExec.execute(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    long lStartTime = System.currentTimeMillis();
+                    command.run();
+                    long lTimeUse = System.currentTimeMillis() - lStartTime;
+                    if (lTimeUse > 1000) {
+                        logger.info("asyncExec时间过长:{}, {}", lTimeUse, command);
+                    }
+                } catch (Exception e) {
+                    logger.error("GGame asyncExec catch- ", e);
+                }
+            }
+        });
+    }
+
     @Override
     protected void doStart() throws Throwable {
         // 1.初始化Properties配置