Creating Thread Groups

There are two constructors that create new thread groups:

ThreadGroup(String name)

Creates a thread group with the given name.

ThreadGroup(ThreadGroup parent, String name)

Creates a thread group that descends from the given parent and has the given name.

In the case of the first constructor, the new thread group is a child of the current thread’s thread group; in the second case, the new thread group is inserted into the thread group hierarchy with the given thread group as its parent. (Though it’s probably bad design to do so, by default a thread group can be inserted anywhere in a Java application’s thread group hierarchy.) In Java 1.0, only Java applications were allowed to create thread groups; this restriction no longer applies.

Each of these constructors creates an empty thread group—a thread group with no threads. There is no method to move a thread into a particular group; a thread is placed into a group only when the thread object is created. As this restriction implies, there are some additional constructors for the Thread class that specify the thread group to which the thread should belong:

Thread(ThreadGroup group, String name)

Constructs a new thread that belongs to the given thread group and has the given name.

Thread(ThreadGroup group, Runnable target)

Constructs a new thread that belongs to the given thread group and runs the given target object.

Thread(ThreadGroup group, Runnable target, String name)

Constructs a new thread that belongs to the given thread group, runs the given target object, and has the given name.

Note that there is no constructor that takes just a ThreadGroup as a parameter, which seems to be an oversight. In the constructors we learned about in Chapter 2, the thread becomes a member of the same thread group to which the current thread belongs.

Similarly, there is no method by which a thread can be deleted from a thread group: a thread is a member of its thread group for the duration of its life. However, when the thread terminates, it is removed automatically from the thread group.

We can use these constructors to modify the TCPServer class so that each client is placed in a separate thread group as well as being run in a separate thread. Doing so is simple: we need only create the thread group immediately before creating the client thread, so that when the client thread is started, it is a member of the new thread group:

import java.net.*;
import java.io.*;

public class TCPServer implements Cloneable, Runnable {
    Thread runner = null;
    ServerSocket server = null;
    Socket data = null;
    volatile boolean shouldStop = false;
    ThreadGroup group = null;
                int groupNo = 0;

    public synchronized void startServer(int port) throws IOException {
        if (runner == null) {
            server = new ServerSocket(port);
            runner = new Thread(this);
            runner.start();
        }
    }

    public synchronized void stopServer() {
        if (server != null) {
            shouldStop = true;
            runner.interrupt();
            runner = null;
            try {
                server.close();
            } catch (IOException ioe) {}
            server = null;
        }
    }

    public void run() {
        if (server != null) {
            while (!shouldStop) {
                try {
                    Socket datasocket = server.accept();
                    TCPServer newSocket = (TCPServer) clone();

                    newSocket.server = null;
                    newSocket.data = datasocket;
                    newSocket.group =
                                    new ThreadGroup("Client Group " + groupNo++);
                                newSocket.runner =
                                    new Thread(newSocket.group, newSocket);
                    newSocket.runner.start();
                } catch (Exception e) {}
            }
        } else {
            run(data);
        }
    }
 
    public void run(Socket data) {
    }
}

Remember that the TCPServer is subclassed in order to provide functionality for the client; in the next section, we’ll look at how this thread group makes it easier to program the code that handles the client.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset