Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Briefly explain the code in area with a /*comment*/ above it /** * List all

ID: 3755553 • Letter: J

Question

JAVA Briefly explain the code in area with a /*comment*/ above it

/**

* List all thread groups and threads in each group in the JVM.

*

* @author Gagne, Galvin, Silberschatz

* Operating System Concepts with Java - Eighth Edition

* Copyright John Wiley & Sons - 2010.

*/

public class ThreadLister

{

ThreadGroup rootThreadGroup = null;

/* Comment Here */

ThreadGroup getRootThreadGroup( ) {

if ( rootThreadGroup != null )

return rootThreadGroup;

ThreadGroup currentThreadGroup = Thread.currentThread( ).getThreadGroup( );

ThreadGroup parentThreadGroup;

while ( (parentThreadGroup = currentThreadGroup.getParent( )) != null )

currentThreadGroup = parentThreadGroup;

rootThreadGroup = currentThreadGroup;

return rootThreadGroup;

}

/* Comment Here */

ThreadGroup[] getAllThreadGroups( ) {

final ThreadGroup root = getRootThreadGroup( );

/* Comment Here */

int alloc = root.activeGroupCount( );

/* Comment Here */

int n = 0;

ThreadGroup[] groups;

do {

/* Comment Here */

alloc *= 2;

groups = new ThreadGroup[alloc];

/* Comment Here */

n = root.enumerate(groups, true);

} while (n == alloc);

/* Comment Here */

ThreadGroup[] allGroups = new ThreadGroup[n+1];

/* Comment Here */

allGroups[0] = root;

/* Comment Here */

System.arraycopy( groups, 0, allGroups, 1, n );

return allGroups;

}

public static void main(String[] args) {

new CreateThreadGroups();

ThreadLister groups = new ThreadLister();

/* Comment Here */

ThreadGroup[] groupList = groups.getAllThreadGroups();

/* Comment Here */

for (int i = 0; i < groupList.length; i++) {

/* Comment Here */

Thread list[] = new Thread[groupList[i].activeCount() * 2];

groupList[i].enumerate(list, false);

/* Comment Here */

System.out.println(groupList[i].getName());

for (int j = 0; j < list.length; j++) {

if (list[j] != null)

System.out.println(" "+list[j].getName()+":"+list[j].getId()+":"+list[j].getState()+":"+list[j].isDaemon());

}

}

}

}

Explanation / Answer

public class ThreadLister { // This is the only group created for a particular object of // threadLister class ThreadGroup rootThreadGroup = null; /* Getting the root thread group */ ThreadGroup getRootThreadGroup() { // this method is based on singleton object.. // if group is already created, return that // else, create a new one and then return if (rootThreadGroup != null) return rootThreadGroup; // it starts finding the group of current thread // and then recursively climbs up the chain to fin the // root thread group ThreadGroup currentThreadGroup = Thread.currentThread().getThreadGroup(); ThreadGroup parentThreadGroup; while ((parentThreadGroup = currentThreadGroup.getParent()) != null) { // make parent group as the current and try to find better parent, if possible currentThreadGroup = parentThreadGroup; } // assign the find group to the root variable rootThreadGroup = currentThreadGroup; return rootThreadGroup; } /* Getting a list of all thread groups */ ThreadGroup[] getAllThreadGroups() { // get the root thread group final ThreadGroup root = getRootThreadGroup(); /* returns a no. of active group in this thread group. */ int alloc = root.activeGroupCount(); /* Comment Here */ int n = 0; ThreadGroup[] groups; do { /* below we create a new group having double the active groups */ // double the active count alloc *= 2; // create group for the new count groups = new ThreadGroup[alloc]; // Copies into the specified array,i.e. root, references to every active subgroup groups threadgroup // true is passed to do it recursively // it returns the number of thread groups put into the array n = root.enumerate(groups, true); // we keep doing this till the time we get desired number of groups in thread group } while (n == alloc); /* create new threadgroup array, we create it to be one larger, to store root reference also */ ThreadGroup[] allGroups = new ThreadGroup[n + 1]; /* store root at index 0 */ allGroups[0] = root; /* copy other groups from groups variable to allGroups starting from index 1 for count of n groups*/ System.arraycopy(groups, 0, allGroups, 1, n); return allGroups; } public static void main(String[] args) { new CreateThreadGroups(); ThreadLister groups = new ThreadLister(); /* Getting a thread group by name */ ThreadGroup[] groupList = groups.getAllThreadGroups(); /* Iterate over all the groups */ for (int i = 0; i