806 Part II: The Java Library
return sum;
}
}
class Hypot implements Callable<Double> {
double side1, side2;
Hypot(double s1, double s2) {
side1 = s1;
side2 = s2;
}
public Double call() {
return Math.sqrt((side1*side1) + (side2*side2));
}
}
class Factorial implements Callable<Integer> {
int stop;
Factorial(int v) { stop = v; }
public Integer call() {
int fact = 1;
for(int i = 2; i <= stop; i++) {
fact *= i;
}
return fact;
}
}
The output is shown here:
Starting
55
5.0
120
Done
The TimeUnit Enumeration
The concurrent API defines several methods that take an argument of typeTimeUnit,
whichindicates a time-out period.TimeUnitis an enumeration that is used to specify the
granularity(or resolution) of the timing.TimeUnitis defined withinjava.util.concurrent.
It can be one of the following values:
DAYS
HOURS
MINUTES
SECONDS
MICROSECONDS
MILLISECONDS
NANOSECONDS