Manipulating Thread Groups

One of the really useful ideas behind a thread group is the ability to manipulate all of its threads at once. There are four methods in the ThreadGroup class that allow us to do just that; however, since three of them are now deprecated, this idea is not as useful as it once was:

void suspend() (deprecated in Java 2)

Suspends all threads that descend from this thread group.

void resume() (deprecated in Java 2)

Resumes all threads that descend from this thread group.

void stop() (deprecated in Java 2)

Stops all threads that descend from this thread group.

void interrupt() ( Java 2 and above only)

Interrupts all threads that descend from this thread group.

These methods all function in the same way as their counterparts in the Thread class, but they affect all threads in the thread group as well as all threads that are contained in the thread groups that descend from this group. In other words, these methods operate recursively on all groups that descend from the specified group. In the case of our TCPServer thread group hierarchy, this means that if, for example, we interrupted the Client1 thread group, we interrupt all threads in that group as well as the I/O threads in the Client1-created thread group.[14]

We can use these calls to save some programming when we create the subclass of our TCPServer. In our ServerHandler subclass, we left out the processing that is performed on behalf of the client. This time, we’ll assume that the server reads a set ...

Get Java Threads, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.