优化ThreadUtil.safeSleep,使用System.nanoTime()

This commit is contained in:
Looly
2024-03-26 18:13:46 +08:00
parent 8e26dd001b
commit 2f7938492e
3 changed files with 18 additions and 11 deletions

View File

@@ -1,5 +1,6 @@
package cn.hutool.core.thread;
import cn.hutool.core.util.RandomUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -11,4 +12,13 @@ public class ThreadUtilTest {
ThreadUtil.execute(() -> Assert.assertTrue(isValid));
}
@Test
public void safeSleepTest() {
final long sleepMillis = RandomUtil.randomLong(1, 1000);
// 随机sleep时长确保sleep时间足够
final long l = System.currentTimeMillis();
ThreadUtil.safeSleep(sleepMillis);
Assert.assertTrue(System.currentTimeMillis() - l >= sleepMillis);
}
}