All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.foreach.across.modules.debugweb.controllers.ThreadsController Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014 the original author or authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.foreach.across.modules.debugweb.controllers;

import com.foreach.across.modules.debugweb.DebugWeb;
import com.foreach.across.modules.debugweb.mvc.DebugMenuEvent;
import com.foreach.across.modules.debugweb.mvc.DebugWebController;
import org.springframework.context.event.EventListener;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@DebugWebController
public class ThreadsController
{
	@EventListener
	public void buildMenu( DebugMenuEvent event ) {
		event.builder().item( "/threadStack", "Threads and stack" );
	}

	@RequestMapping("/threadStack")
	public String showThreads( Model model ) {
		List threadGroups = loadThreadData();

		StringBuilder output = new StringBuilder();
		output.append( "
" ); output.append( printThreads( threadGroups ) ); output.append( "
" ); Thread[] allThreads = getAllThreads( threadGroups ); if ( ( allThreads == null ) || ( allThreads.length < 1 ) ) { output.append( "SystemThreadList: main(): allThreads is null or length < 1." ); } else { // Print thread info for ( int i = 0; i < allThreads.length; i++ ) { Thread t = allThreads[i]; output.append( "Thread " ).append( i ).append( " = " ).append( t.toString() ).append( "
\n" ); output.append( "    state: " ).append( t.getState().toString() ).append( " - prio: " ).append( t.getPriority() ).append( " - alive: " + t.isAlive() + " - daemon: " + t.isDaemon() + " - interrupted: " ).append( t.isInterrupted() ).append( "
" ); StackTraceElement[] stack = t.getStackTrace(); for ( int j = 0; j < stack.length; j++ ) { output.append( "frame: " ).append( j ).append( ": " ).append( stack[j].toString() ).append( "
" ); } output.append( "
" ); } } model.addAttribute( "threadOutput", output ); return DebugWeb.VIEW_THREADS; } private ArrayList loadThreadData() { ArrayList threadList = new ArrayList(); ThreadGroup tg = Thread.currentThread().getThreadGroup(); if ( tg != null ) { threadList.add( tg ); while ( tg != null && tg.getParent() != null ) { tg = tg.getParent(); if ( tg != null ) { threadList.add( tg ); } } } return threadList; } private String printThreads( List threads ) { StringBuilder sb = new StringBuilder( "[SystemThreadList:\n
" ); int threadCount = getThreadCount( threads ); if ( threadCount < 1 ) { sb.append( " No Threads " ); } else { for ( int i = 0; i < threadCount; i++ ) { sb.append( " ThreadGroup " ).append( i ).append( "= " ); sb.append( getThreadGroup( i, threads ).toString() ); sb.append( ", activeCount = " ).append( getThreadGroup( i, threads ).activeCount() ); sb.append( "\n
" ); } } // Total active count sb.append( " totalActiveCount = " ).append( getTotalActiveCount( threads ) ).append( "\n
" ); sb.append( " (End of SystemThreadList)]\n
" ); return sb.toString(); } private Thread[] getAllThreads( List threadList ) { int estimatedCount = getTotalActiveCount( threadList ); // Start with array twice size of estimated, // to be safe. Trim later. Thread[] estimatedThreads = new Thread[estimatedCount * 2]; // Locate root group ThreadGroup rootGroup = getRootThreadGroup( threadList ); if ( rootGroup == null ) { return null; } int actualCount = rootGroup.enumerate( estimatedThreads, true ); // Check that something was returned if ( actualCount < 1 ) { return null; } // Copy into actualThreads of correct size Thread[] actualThreads = new Thread[actualCount]; System.arraycopy( estimatedThreads, 0, actualThreads, 0, actualCount ); return actualThreads; } private ThreadGroup getRootThreadGroup( List threadList ) { if ( getThreadCount( threadList ) < 1 ) { return null; } else { ThreadGroup tg; for ( int i = 0; i < getThreadCount( threadList ); i++ ) { tg = getThreadGroup( i, threadList ); if ( tg.getParent() == null ) { return tg; } } // If we got here, we didn't find one, so return null. return null; } } private ThreadGroup getThreadGroup( int index, List threadList ) { if ( getThreadCount( threadList ) < 1 ) { return null; } else if ( ( index < 0 ) || ( index > ( getThreadCount( threadList ) - 1 ) ) ) { return null; } else { return threadList.get( index ); } } private int getThreadCount( List threadList ) { if ( threadList == null ) { return -1; } else { return threadList.size(); } } private int getTotalActiveCount( List threadList ) { if ( getThreadCount( threadList ) < 1 ) { return 0; } else { int totalActiveCount = 0; ThreadGroup tg; for ( int i = 0; i < getThreadCount( threadList ); i++ ) { tg = getThreadGroup( i, threadList ); totalActiveCount += tg.activeCount(); } return totalActiveCount; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy