~ read.

Better option for common Java Thread.sleep()

Usually when i need to put some defined waits in Java( i know that it's not good practice, but sometimes it's actually what is needed) i use all well known Thread.sleep(), but what i don't like in this method is that you have to specify time duration in milliseconds. 1 second == 1000 miliseconds. And that's fine, but when you need to set wait for 3 minutes, then Thread.sleep(180000) is not that easy to read anymore, right ? So, to make your code more readable, you can use TimeUnit class. Here's how it looks :

Thread.sleep(180000) == TimeUnit.MINUTE.sleep(3) // wait for 3 minutes  
Thread.sleep(30000) == TimeUnit.SECOND.sleep(30) // wait for 30 seconds  

As you can see, it's much better to use TimeUnit in your code.

comments powered by Disqus
comments powered by Disqus