Obtains all threads currently running in Java VM.
Program running diagram:
The following static method can return all the threads currently running in Java VM using arrays.
Public static thread [] findallthreads (){
Threadgroup group =
Thread. currentthread (). getthreadgroup ();
Threadgroup topgroup = group;
// Traverse the thread group tree to obtain the root thread group
While (group! = NULL ){
Topgroup = group;
Group = group. getparent ();
}
// Double the number of activated threads
Int estimatedsize = topgroup. activecount () * 2;
Thread [] slacklist = new thread [estimatedsize];
// Obtain all threads in the root thread group
Int actualsize = topgroup. enumerate (slacklist );
// Copy into a list that is the exact size
Thread [] list = new thread [actualsize];
System. arraycopy (slacklist, 0, list, 0, actualsize );
Return list;
}
The threadviewer. Java program graphically displays all the threads currently running in the Java VM. It is automatically refreshed every 2 seconds to keep the latest information available. Program threadviewer. Java source code:
Package thread; <br/> Import Java. AWT. *; <br/> Import Java. AWT. event. *; <br/> Import javax. swing. *; <br/> Import javax. swing. table. *; <br/> public class threadviewer extends jpanel {<br/> private threadviewertablemodel tablemodel; <br/> Public threadviewer () {<br/> tablemodel = new threadviewertablemodel (); <br/> jtable table = new jtable (tablemodel); <br/> table. setautoresizemode (jtable. auto_resize_last_column); <br/> tablecolumnmodel colmodel = table. getcolumnmodel (); <br/> int numcolumns = colmodel. getcolumncount (); <br/> // manually size all but the last column <br/> for (INT I = 0; I <numcolumns-1; I ++) {<br/> tablecolumn Col = colmodel. getcolumn (I); <br/> Col. sizewidthtofit (); <br/> Col. setpreferredwidth (Col. getwidth () + 5); <br/> Col. setmaxwidth (Col. getwidth () + 5); <br/>}< br/> jscrollpane sp = new jscrollpane (table); <br/> setlayout (New borderlayout ()); <br/> Add (SP, borderlayout. center); <br/>}< br/> Public void dispose () {<br/> tablemodel. stoprequest (); <br/>}< br/> protected void finalize () throws throwable {<br/> dispose (); <br/>}< br/> Public static jframe createframedinstance () {<br/> final threadviewer viewer = new threadviewer (); <br/> final jframe F = new jframe ("threadviewer"); <br/> F. addwindowlistener (New windowadapter () {<br/> Public void windowclosing (invalid wevent e) {<br/> F. setvisible (false); <br/> F. dispose (); <br/> viewer. dispose (); <br/>}< br/>}); <br/> F. setcontentpane (viewer); <br/> F. setsize (500,300); <br/> F. setvisible (true); <br/> return F; <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> jframe F = threadviewer. createframedinstance (); <br/> // for this example, exit the VM when the viewer <br/> // frame is closed. <br/> F. addwindowlistener (New windowadapter () {<br/> Public void windowclosing (invalid wevent e) {<br/> system. exit (0); <br/>}< br/> }); <br/> // keep the main thread from exiting by blocking <br/> // on wait () for a notification that never comes. <br/> Object lock = new object (); <br/> synchronized (LOCK) {<br/> try {<br/> lock. wait (); <br/>}catch (interruptedexception X) {<br/>}< br/>
Program threadviewertablemodel. Java source code:
Package thread; <br/> Import Java. AWT. *; <br/> Import Java. lang. reflect. *; <br/> Import javax. swing. *; <br/> Import javax. swing. table. *; <br/> public class threadviewertablemodel extends acttablemodel {<br/> private object datalock; <br/> private int rowcount; <br/> private object [] [] celldata; <br/> private object [] [] pendingcelldata; <br/> // The column information remains constant <br/> private final Int columncount; <br/> private final string [] columnname; <br/> private final class [] columnclass; <br/> // self-running object control variables <br/> private thread internalthread; <br/> private volatile Boolean nostoprequested; <br/> Public threadviewertablemodel () {<br/> rowcount = 0; <br/> celldata = new object [0] [0]; <br/> // jtable uses this information for the column headers <br/> string [] names = {<Br/> "Priority", "alive", <br/> "daemon", "interrupted", <br/> "threadgroup", "thread name "}; <br/> columnname = names; </P> <p> // jtable uses this information for cell rendering <br/> class [] classes ={< br/> integer. class, Boolean. class, <br/> Boolean. class, Boolean. class, <br/> string. class, String. class };< br/> columnclass = classes; <br/> columncount = columnname. length; <br/> // used to control Co Ncurrent access <br/> datalock = new object (); <br/> nostoprequested = true; <br/> runnable r = new runnable () {<br/> Public void run () {<br/> try {<br/> runwork (); <br/>} catch (exception X) {<br/> // In case any exception slips through <br/> X. printstacktrace (); <br/>}< br/>}; <br/> internalthread = new thread (R, "threadviewer "); <br/> internalthread. setpriority (thread. max_priority-2); <br/> Internalthread. setdaemon (true); <br/> internalthread. start (); <br/>}< br/> private void runwork () {<br/> // The run () method of transferpending is called by <br/> // The event handling thread for safe concurrency. <br/> runnable transferpending = new runnable () {<br/> Public void run () {<br/> transferpendingcelldata (); <br/> // Method of Using acttablemodel that <br/> // causes the table to be updated. <Br/> firetabledatachanged (); <br/>}< br/>}; <br/> while (nostoprequested) {<br/> try {<br/> creatependingcelldata (); <br/> swingutilities. invokeandwait (transferpending); <br/> thread. sleep (2000); <br/>} catch (invocationtargetexception Tx) {<br/> Tx. printstacktrace (); <br/> stoprequest (); <br/>} catch (interruptedexception X) {<br/> thread. currentthread (). interrupt (); <br/>}< br/> P Ublic void stoprequest () {<br/> nostoprequested = false; <br/> internalthread. interrupt (); <br/>}< br/> Public Boolean isalive () {<br/> return internalthread. isalive (); <br/>}< br/> private void creatependingcelldata () {<br/> // this method is called by the Internal Thread <br/> thread [] thread = findallthreads (); <br/> object [] [] Cell = new object [thread. length] [columncount]; <br/> for (INT I = 0; I <Thread. length; I ++) {<br/> thread t = thread [I]; <br/> object [] rowcell = cell [I]; <br/> rowcell [0] = new INTEGER (T. getpriority (); <br/> rowcell [1] = new Boolean (T. isalive (); <br/> rowcell [2] = new Boolean (T. isdaemon (); <br/> rowcell [3] = new Boolean (T. isinterrupted (); <br/> rowcell [4] = T. getthreadgroup (). getname (); <br/> rowcell [5] = T. getname (); <br/>}< br/> synchronized (datalock) {<br/> pendi Ngcelldata = cell; <br/>}< br/> private void transferpendingcelldata () {<br/> // this method is called by the event thread <br/> synchronized (datalock) {<br/> celldata = pendingcelldata; <br/> rowcount = celldata. length; <br/>}< br/> Public int getrowcount () {<br/> // this method is called by the event thread <br/> return rowcount; <br/>}</P> <p> Public object getvalueat (INT row, int col) {<Br/> // this method is called by the event thread <br/> return celldata [row] [col]; <br/>}< br/> Public int getcolumncount () {<br/> return columncount; <br/>}< br/> public class getcolumnclass (INT columnidx) {<br/> return columnclass [columnidx]; <br/>}< br/> Public String getcolumnname (INT columnidx) {<br/> return columnname [columnidx]; <br/>}< br/> Public static thread [] findallthreads () {<br/> THR Eadgroup group = <br/> thread. currentthread (). getthreadgroup (); <br/> threadgroup topgroup = group; <br/> // traverse the threadgroup tree to the top <br/> while (group! = NULL) {<br/> topgroup = group; <br/> group = group. getparent (); <br/>}< br/> // create a destination array that is about <br/> // twice as big as needed to be very confident <br/> // that none are clipped. <br/> int estimatedsize = topgroup. activecount () * 2; <br/> thread [] slacklist = new thread [estimatedsize]; <br/> // load the thread references into the oversized <br/> // array. the actual number of threads loaded <br/> // is returned. <br/> int actualsize = topgroup. enumerate (slacklist); <br/> // copy into a list that is the exact size <br/> thread [] list = new thread [actualsize]; <br/> system. arraycopy (slacklist, 0, list, 0, actualsize); <br/> return list; <br/>}< br/>
Http://www.java3z.com/cwbwebhome/article/article5a/5138.html? Id = 266