Mega Code Archive

 
Categories / Java Book / 007 Thread Conncurrent
 

0407 The TimeUnit Enumeration

The concurrent API uses TimeUnit to indicate a time-out period. TimeUnit is an enumeration that is used to specify the granularity of the timing. TimeUnit is defined within java.util.concurrent. It can be one of the following values: DAYS HOURS MINUTES SECONDS MICROSECONDS MILLISECONDS NANOSECONDS The TimeUnit enumeration defines various methods that convert between units. These are shown here: long convert(long tval, TimeUnit tu) long toMicros(long tval) long toMillis(long tval) long toNanos(long tval) long toSeconds(long tval) long toDays(long tval) long toHours(long tval) long toMinutes(long tval) The convert( ) method converts tval into the specified unit and returns the result. The to methods perform the indicated conversion and return the result. TimeUnit also defines the following timing methods: void sleep(long delay) throws InterruptedExecution sleep( ) pauses execution for the specified delay period. It translates into a call to Thread.sleep( ). void timedJoin(Thread thrd, long delay) throws InterruptedExecution similar with Thread.join( ) in which pauses for the time period specified by delay, which is described in terms of the invoking time unit. void timedWait(Object obj, long delay) throws InterruptedExecution a specialized version of Object.wait( ) in which obj is waited on for the period of time specified by delay, which is described in terms of the invoking time unit.