The
TimeUnit Enumeration
The concurrent API defines
several methods that take an argument of type TimeUnit, which indicates a time-out period. TimeUnit is an enumeration that is used to specify the granularity (or resolution) 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
Although TimeUnit lets you specify any of these values in calls to methods
that take a timing argument, there is no guarantee that the system is capable
of the specified resolution.
Here is an example that uses TimeUnit. The CallableDemo class, shown in the previous section, is modified as
shown next to use the second form of get(
) that takes a TimeUnit
argument.
try {
System.out.println(f.get(10,
TimeUnit.MILLISECONDS)); System.out.println(f2.get(10, TimeUnit.MILLISECONDS));
System.out.println(f3.get(10, TimeUnit.MILLISECONDS));
} catch (InterruptedException exc) {
System.out.println(exc);
}
catch (ExecutionException exc) {
System.out.println(exc);
} catch (TimeoutException exc) {
System.out.println(exc);
}
In this version, no call to get( ) will wait more than 10
milliseconds.
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
void timedJoin(Thread thrd, long delay) throws InterruptedExecution void timedWait(Object obj, long delay) throws InterruptedExecution
Here, sleep( ) pauses execution for the specified delay period, which is
specified in terms of the invoking enumeration constant. It translates into a
call to Thread.sleep( ). The timedJoin( ) method is a specialized
version of Thread.join( ) in which thrd pauses for the time period
specified by delay, which is
described in terms of the invoking time unit. The timedWait( ) method is 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.
The
Concurrent Collections
As explained, the concurrent
API defines several collection classes that have been engineered for concurrent
operation. They include:
ArrayBlockingQueue
ConcurrentHashMap
ConcurrentLinkedDeque ConcurrentLinkedQueue ConcurrentSkipListMap
ConcurrentSkipListSet CopyOnWriteArrayList CopyOnWriteArraySet DelayQueue
LinkedBlockingDeque LinkedBlockingQueue LinkedTransferQueue
PriorityBlockingQueue SynchronousQueue
These offer concurrent
alternatives to their related classes defined by the Collections Framework.
These collections work much like the other collections except that they provide
concurrency support. Programmers familiar with the Collections Framework will
have no trouble using these concurrent collections.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.