Thread Group creates a group of threads. It define,s these two constructors: Threw Group(String group Nate) Thread group Thread Croup parental, String group ) For both forms, group Canal specifies the name of the thread group. The first version creates a new group that has the current thread as its parent. In the second form, the parent is specified.
The methods defined by’ Thread Group are shown in Table 14-17. In versions of Java prior to 2, Thread Group also included the methods stop l, suspend( I, and sureness ). These have been deprecated by java 2 because-they were inherently unstable. Thread groups offer: a convenient way to manage groups of threads as a Unfit. This is particularly valuable in situations in which you want to suspend and resume n number of related threads. For example, imagine a program in which one set of threads is used for printing n document, another set is used to display the document on the screen, and another set saves the document to a disk file. If printing is aborted, you will want an ensy way to stop all threads related to printing.Th rend groups offer this convenience. The following program, which creates two thread groups of two threads each, illustrates this usage:
Inside the program, notice that thread group A is suspended for four seconds. A.s , 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 me obtained by calling enumerate! ) on group A. Then, each thread is suspended by iterating through the resulting army. To resume the threads in A, the list is again traversed and each thread is resumed. One last point: this example uses the recommended Java 2 approach to suspending and resuming threads. It does not rely upon the deprecated methods suspend( ) ‘and resume( )
Thread local and Inherit able Thread Local
Java ? adds two Hew thread-related classes to java.lang: Thread Local Used to create thread local variables. Each thread will have its
copy or a thread 10 variable. noninterchangeable Creates thread local variables that may be inherited.
Java 2 adds a class called Package that encapsulates version data associated with a package. Package erosion 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 Package are shown in Table 14-18. The following program demonstrates Package, displaying the packages about which the gristmill currently is aware.