Example of TimeUnit in Java
February 18, 2013
TimeUnit belongs to the package java.util.concurrent. TimeUnit has come in java since jdk 1.5. TimeUnit playes with unit of time. TimeUnit has many units like DAYS,MINUTES,SECONDS etc.
TimeUnit tu=TimeUnit.DAYS;
TimeUnit.SECONDS.timedJoin(th, 5);
TimeUnitTest.java
package com.concretepage; import java.util.concurrent.TimeUnit; public class TimeUnitTest { public static void main(String... args) throws InterruptedException{ TimeUnit tu=TimeUnit.DAYS; long noOfDays=tu.convert(48,TimeUnit.HOURS); System.out.println("noOfDays:"+noOfDays); TimeUnit.SECONDS.sleep(2); Thread th=new Thread( new TimeUnitTest().new RunnableThread()); th.start(); TimeUnit.SECONDS.timedJoin(th, 5); System.out.println("done"); } //runnable thread class RunnableThread implements Runnable { @Override public void run() { int cnt = 0; for (; cnt < 5;cnt++ ) { System.out.println("run:" + cnt); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } } }