Question: Java: Briefly explain the code in each area with a /*comment*/ above it public class ThreadLister { ThreadGroup rootThreadGroup = null; /* Getting the root

Java: Briefly explain the code in each area with a /*comment*/ above it

public class ThreadLister { ThreadGroup rootThreadGroup = null; /* Getting the root thread group*/ 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; }

/* Getting a list of all thread groups*/ 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();

/* Getting a thread group by name */ 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("\t"+list[j].getName()+":"+list[j].getId()+":"+list[j].getState()+":"+list[j].isDaemon()); } } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!