$25
Write a Java program that lists all threads
in the Java virtual machine. Before
proceeding, you will need some background information. All threads in the JVM belong to a thread
group, and a thread group is identified in the Java API by the ThreadGroup
class. Thread groups are organized as a
tree structure, where the root of the tree is the system thread group. The system thread group contains threads that
are automatically created by the JVM, mostly for managing object references.
Below the system thred group is the main thread group. The main thread group contains the initial
thread in a Java program that begins executing in the main() method. It is also
the default thread group, meaning that – unless otherwise specified – all
threads you create belong to this group.
It is possible to create additional thread groups and assign newly
created threads to these groups.
Furthermore, when creating a thread group, you may specify its
parent. For example, the following
statements create three new thread groups: alpha, beta, and theta:
ThreadGroup
alpha = new ThreadGroup(“alpha”);
ThreadGroup
alpha = new ThreadGroup(“beta”);
ThreadGroup
alpha = new ThreadGroup(alpha, “theta”);
The
alpha and beta groups belong to the default – or main – thread group. However, the constructor for the theta thread
group indicates that its parent group is alpha.
Thus, we have the thread-group hierarchy shown in Figure 1.
Figure
1. Thread-group hierarchy
Notice that all thread groups have a
parent, with the obvious exception of the system thread group, whose parent is
null.
Writing a program that lists all threads in
the Java virtual machine will involve a careful reading of the Java API – in
particular, the java.lang.ThreadGroup and java.lang.Thread classes. A strategy
for constructing this program is to first identify all thread groups in the JVM
and then identify all threads within each group. To determine all thread groups, first obtain
the ThreadGroup of the current thread, and then ascend the thread-group tree
hierarchy to its root. Next, get all thread groups below the root thread group;
you should find the overloaded enumerate() method in the ThreadGroupd class
especially helpful. Next, identify the threads belonging to each group. Again,
the enumerate() method should prove helpful.
One thing to be careful of when using the enumerate() method is that it
expectes an array as a parameter. This
means that you will need to determine the size of the array before calling
enumerate(). Additional methods in the
ThreadGroup API should be useful for determining how to size arrays. Have your output list each thread group and
all threads within each group. For
example, based on Figure 1, your output should appear as follows:
·
system: all threads in the
system group
·
main: all threads in the main
group
·
alpha: all threads in the alpha
group
·
beta: all threads in the beta
group
·
theta: all threads in the theta
group
when outputting each thread, list the
following fields:
·
name of the thread group
·
the thread name
·
the thread identifier
·
the state of the thread
·
whether or not the thread is a
daemon
·
priority of the thread
Your program will periodically refresh the
listing of threads and thread groups by specifying a refresh parameter on the
command line when invoking the program. Represent this parameter in
milliseconds. For example, if your program is named ThreadLister, to invoke the
program so that it refreshes the list ten times per second, enter the
following:
java
ThreadLister 100
The following source code is an example
program (CreateThreadGroups.java) that creates the alpha, beta, and theta
thread groups as well as several threads within each group. To use this program, enter the statement
new
CreateThreadGroups();
at the beginning of your thread-listing
program.
/**
* A
program that tests your implementation that
*
lists all thread groups
* -
and threads within each group -
* in
the JVM.
*/
public class CreateThreadGroups
{
public
CreateThreadGroups() {
//
create a few thread groups
ThreadGroup
alpha = new ThreadGroup("alpha");
ThreadGroup
beta = new ThreadGroup("beta");
ThreadGroup
theta = new ThreadGroup(alpha, "theta");
//
create a few threads belonging to the above thread groups
(new
Thread(alpha, new GroupWorker())).start();
(new
Thread(alpha, new GroupWorker())).start();
(new
Thread(alpha, new GroupWorker())).start();
(new
Thread(beta, new GroupWorker())).start();
(new
Thread(theta, new GroupWorker())).start();
(new
Thread(theta, new GroupWorker())).start();
}
class
GroupWorker implements Runnable
{
public
void run() {
while
(true) {
try
{
Thread.sleep(1000);
for
(int i = 0; i < 1000000; i++)
;
}
catch
(InterruptedException ie) { }
}
}
}
}
Grading
Policy
Input test
cases
Results
Points
Compile and runs the program
The list thread program can compile and run
successfully
Display the thread name
The thread name can display successfully
Display the thread identifier
The thread identifier can display successfully
Display the state of the thread
The state of the thread can display successfully
Display whether or not the thread is a daemon
The daemon flag can display successfully
Display priority of the thread
The priority of the thread can display
successfully
Refresh the thread list
The list of threads are refreshed ten times per
second
documentation
1-5 questions