Two exiting.
Main thread exiting.
Inside the program, notice that thread group A is suspended for four seconds. As the
output confirms, this causes threads One and Two to pause, but threads Three and Four
continue running. After the four seconds, threads One and Two are resumed. Notice how
thread group A is suspended and resumed. First, the threads in group A are obtained by
callingenumerate( )on group A. Then, each thread is suspended by iterating through the
resulting array. To resume the threads in A, the list is again traversed and each thread is
resumed. One last point: this example uses the recommended approach to suspending
and resuming threads. It does not rely upon the deprecated methodssuspend( )and
resume( ).
ThreadLocal and InheritableThreadLocal
Java defines two additional thread-related classes injava.lang:
- ThreadLocal Used to create thread local variables. Each thread will have its own
copy of a thread local variable. - InheritableThreadLocal Creates thread local variables that may be inherited.
Package
Packageencapsulates version data associated with a package. Package version information
is becoming more important because of the proliferation of packages and because a Java
program may need to know what version of a package is available. The methods defined by
Packageare shown in Table 16-19. The following program demonstratesPackage, displaying
the packages about which the program currently is aware:
// Demonstrate Package
class PkgTest {
public static void main(String args[]) {
Package pkgs[];
pkgs = Package.getPackages();
for(int i=0; i < pkgs.length; i++)
System.out.println(
pkgs[i].getName() + " " +
pkgs[i].getImplementationTitle() + " " +
pkgs[i].getImplementationVendor() + " " +
pkgs[i].getImplementationVersion()
);
}
}
Chapter 16: Exploring java.lang 429