You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by "mxsm (via GitHub)" <gi...@apache.org> on 2023/01/26 14:15:16 UTC

[GitHub] [rocketmq] mxsm commented on a diff in pull request #5925: [ISSUE #5924]Optimize UtilAll#sleep method

mxsm commented on code in PR #5925:
URL: https://github.com/apache/rocketmq/pull/5925#discussion_r1087906656


##########
common/src/main/java/org/apache/rocketmq/common/UtilAll.java:
##########
@@ -83,15 +84,18 @@ public static int getPid() {
     }
 
     public static void sleep(long sleepMs) {
-        if (sleepMs < 0) {
+        sleep(sleepMs, TimeUnit.MILLISECONDS);
+    }
+
+    public static void sleep(long timeOut, TimeUnit timeUnit) {
+        if (null == timeUnit) {
             return;
         }
         try {
-            Thread.sleep(sleepMs);
-        } catch (Throwable ignored) {
+            timeUnit.sleep(timeOut);
+        } catch (InterruptedException ignored) {

Review Comment:
   @caigy 156
   timeUnit.sleep(x)will call Thread.sleep after validating that the timeout is positive. Simplifies the previous code.
   Other than that, the only difference is readability and using TimeUnitis probably easier to understand for non-obvious durations (for example: Thread.sleep(180000) vs. TimeUnit.MINUTES.sleep(3)). and I've adjusted the thrown exception to be Throwable.
   



-- 
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: commits-unsubscribe@rocketmq.apache.org

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