Thread Naming

The next topic we will examine concerns the thread support methods that are used mainly for thread “bookkeeping.” First, it is possible to assign a String name to the Thread object itself:

void setName(String name)

Assigns a name to the Thread instance.

String getName()

Gets the name of the Thread instance.

The Thread class provides a method that allows us to attach a name to the thread object and a method that allows us to retrieve the name. The system does not use this string for any specific purpose, though the name is printed out by the default implementation of the toString() method of the thread. The developer who assigns the name is free to use this string for any purpose desired. For example, let’s assign a name to our TimerThread class:

import java.awt.*;

public class TimerThread extends Thread {
    Component comp;                // Component that needs repainting
    int timediff;                  // Time between repaints of the component
    volatile boolean shouldRun;    // Set to false to stop thread

    public TimerThread(Component comp, int timediff) {
        this.comp = comp;
        this.timediff = timediff;
        shouldRun = true;
        setName("TimerThread(" + timediff + " milliseconds)");
    }

    public void run() {
        while (shouldRun) {
            try {
                comp.repaint();
                sleep(timediff);
            } catch (Exception e) {}
        }
    }
}

In this version of the TimerThread class, we assigned a name to the thread. The name that is assigned is simply “TimerThread” followed by the number of milliseconds used in this timer thread. If the getName() method is later called on this instance, this string value will be returned.

The naming support is also available as a constructor of the Thread class:

Thread(String name)

Constructs a thread object with a name that is already assigned. This constructor is used when threading by inheritance.

Thread(Runnable target, String name)

Constructs a thread object that is associated with the given Runnable object and is created with a name that is already assigned. This constructor is used when threading by interfaces.

Just like the setName() method, setting the name via the thread constructor is simple. One constructor is provided for threading by inheritance and another for threading by interfaces. In our TimerThread example, since we are setting the name in the constructor, we could just as easily have used the thread constructor instead of the setName() method:

import java.awt.*;

public class TimerThread extends Thread {
    Component comp;                 // Component that needs repainting
    int timediff;                   // Time between repaints of the component
    volatile boolean shouldRun;     // Set to false to stop thread

    public TimerThread(Component comp, int timediff) {
        super("TimerThread(" + timediff + " milliseconds)");
        this.comp = comp;
        this.timediff = timediff;
        shouldRun = true;
    }

    public void run() {
        while (shouldRun) {
            try {
                comp.repaint();
                sleep(timediff);
            } catch (Exception e) {}
        }
    }
}
            
..................Content has been hidden....................

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