You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@eventmesh.apache.org by "mxsm (via GitHub)" <gi...@apache.org> on 2023/02/10 03:03:22 UTC

[GitHub] [incubator-eventmesh] mxsm commented on a diff in pull request #3079: [ISSUE #3078]Refactor ThreadUtils

mxsm commented on code in PR #3079:
URL: https://github.com/apache/incubator-eventmesh/pull/3079#discussion_r1102228907


##########
eventmesh-common/src/main/java/org/apache/eventmesh/common/utils/ThreadUtils.java:
##########
@@ -20,22 +20,45 @@
 import org.apache.logging.log4j.util.ProcessIdUtil;
 
 import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.TimeUnit;
 
 public class ThreadUtils {
 
     private static volatile long currentPID = -1;
 
-    public static void randomSleep(int min, int max) throws Exception {
-        // nextInt is normally exclusive of the top value, so add 1 to make it inclusive
-        int random = ThreadLocalRandom.current().nextInt(min, max + 1);
-        Thread.sleep(random);
+    public static void randomSleep(long min, long max) {
+        randomSleep(min, max, TimeUnit.MILLISECONDS);
+    }
 
+    public static void randomSleep(long min, long max, TimeUnit timeUnit) {
+        // nextInt is normally exclusive of the top value, so add 1 to make it inclusive
+        try {
+            long timeout = ThreadLocalRandom.current().nextLong(min, max + 1);
+            timeUnit.sleep(timeout);
+        } catch (InterruptedException ignore) {
+            //ignore
+        }
     }
 
-    public static void randomSleep(int max) throws Exception {
+    public static void randomSleep(long max) {

Review Comment:
   > Suggest to rename method will be better.
   
   @jonyangx Good idea, I will do and resubmit the pr later



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org