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

This commit is contained in:
Looly
2024-03-26 18:13:41 +08:00
parent 3fa431c5c1
commit bdc964c882
2 changed files with 15 additions and 10 deletions

View File

@@ -101,4 +101,13 @@ public class ThreadUtilTest {
}, "线程 - " + x).start();
}
}
@Test
void safeSleepTest() {
final long sleepMillis = RandomUtil.randomLong(1, 1000);
// 随机sleep时长确保sleep时间足够
final long l = System.currentTimeMillis();
ThreadUtil.safeSleep(sleepMillis);
Assertions.assertTrue(System.currentTimeMillis() - l >= sleepMillis);
}
}