How many time have you written the following code in Java?

try {
  Thread.sleep(1000); //sleep for a second
} catch (InterruptedException e) {
  e.printStackTrace();
}

Well, with the introduction of TimeUnit class, there is a better way to do this

try {
  TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
  e.printStackTrace();
}

You do not need the comment anymore. Code is self documenting :). Take a look at the TimeUnit class for more time units.

Advertisements