Project 2 Threads in the Java Virtual Machine

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:

alpha = new ThreadGroup(“alpha”);

alpha = new ThreadGroup(“beta”);

alpha = new ThreadGroup(alpha, “theta”);

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. 








1. Thread-group hierarchy


Notice that all thread groups have a
parent, with the obvious exception of the system thread group, whose parent is

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

alpha: all threads in the alpha

beta: all threads in the beta

theta: all threads in the theta

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

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

ThreadLister 100

The following source code is an example
program ( that creates the alpha, beta, and theta
thread groups as well as several threads within each group.  To use this program, enter the statement


at the beginning of your thread-listing


 * A
program that tests your implementation that

lists all thread groups

 * -
and threads within each group -

 * in
the JVM.




public class CreateThreadGroups


CreateThreadGroups() {

create a few thread groups

alpha = new ThreadGroup("alpha");

beta = new ThreadGroup("beta");

theta = new ThreadGroup(alpha, "theta");


create a few threads belonging to the above thread groups    

Thread(alpha, new GroupWorker())).start();

Thread(alpha, new GroupWorker())).start();

Thread(alpha, new GroupWorker())).start();

Thread(beta, new GroupWorker())).start();

Thread(theta, new GroupWorker())).start();

Thread(theta, new GroupWorker())).start();



GroupWorker implements Runnable


void run() {

(true) {



(int i = 0; i < 1000000; i++)



(InterruptedException ie) { }







Input test



Compile and runs the program

The list thread program can compile and run


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


Refresh the thread list

The list of threads are refreshed ten times per





1-5 questions



Powered by