Why Does My Colleague Use Thread.sleep(0) in their Code

lance
Level Up Coding
Published in
5 min readDec 24, 2022

--

The seemingly useless code is actually very useful.

Recently, I was very confused when I read a piece of code from a colleague. He used the thread sleep method Thread.sleep(long millis)in the code but set the thread sleep time to 0 seconds. Here’s the code.

 int i = 0;
while (i<10000000) {
// business logic…

--

--