ThreadUtil makes the API more specific, so it provides convenience for beginners of Thread Programming, thus simplifying the API. The source code is as follows:
[Java]
Package mybole;
Public class ThreadUtil {
Public static ThreadGroup getSystemThreadGroup ()
{
ThreadGroup systemThreadGroup;
ThreadGroup parentThreadGroup;
SystemThreadGroup = Thread. currentThread (). getThreadGroup ();
While (parentThreadGroup = systemThreadGroup. getParent ())! = Null)
SystemThreadGroup = parentThreadGroup;
Return systemThreadGroup;
}
Public static void printSystemThreadGroup ()
{
System. out. println (getSystemThreadGroup ());
}
Public static Thread getThread ()
{
Return new Thread (new Job ());
}
Public static Thread [] getThreads (int n)
{
Thread [] ta = new Thread [n];
For (int I = 0; I <ta. length; I ++)
{
Ta [I] = getThread ();
Ta [I]. start ();
}
Return ta;
}
Public static ThreadGroup [] getThreadGroup ()
{
ThreadGroup stg = getSystemThreadGroup ();
Int nog = stg. activeGroupCount () + 1;
ThreadGroup [] tga = new ThreadGroup [nog];
Stg. enumerate (tga );
Tga [tga. length-1] = stg;
Return tga;
}
Public static void print (Object [] o)
{
For (Object x: o)
System. out. println (x );
}
Public static void print (Thread [] o)
{
For (Thread x: o)
System. out. println (x + "isAlive =" + x. isAlive () + "name =" + x. getName () + "isDaemon =" + x. isDaemon ());
}
Public static Thread [] getThreads ()
{
ThreadGroup stg = getSystemThreadGroup ();
Thread [] ta = new Thread [stg. activeCount ()];
Stg. enumerate (ta, true );
Return ta;
}
Public static void setPriority (Thread [] ta, int p)
{
For (Thread x: ta)
X. setPriority (p );
}
Public static void setName (Thread [] ta, String n)
{
For (Thread x: ta)
X. setName (n );
}
Public static void main (String [] args)
{
System. out. println ("Hello Thread !! ");
Thread [] ta = getThreads ();
<Span style = "white-space: pre "> </span> *************** **
<Span style = "white-space: pre"> </span> // Thread [] ta = getThreads (3 );
Print (ta );
SetPriority (ta, 10 );
SetName (ta, "blueln ");
Print (ta );
}
}
[Java]
Package mybole;
Public class Job implements Runnable
{
Static int totalNumberOfJobs = 0;
Int jobNumber = 0;
Job ()
{
JobNumber = totalNumberOfJobs;
TotalNumberOfJobs ++;
}
Public void run ()
{
While (true)
{
System. out. println ("t # =" + jobNumber + "\ t" + new java. util. Date ());
Try
{
Thread. sleep (int) (Math. random () * 10000 ));
}
Catch (InterruptedException e)
{
E. getMessage ();
}
}
}
}
Output result:
[Java]
Hello Thread !!
Thread [Reference Handler, 10, system] isAlive = true name = Reference Handler isDaemon = true
Thread [Finalizer, 8, system] isAlive = true name = Finalizer isDaemon = true
Thread [Signal Dispatcher, 9, system] isAlive = true name = Signal Dispatcher isDaemon = true
Thread [Attach Listener, 5, system] isAlive = true name = Attach Listener isDaemon = true
Thread [main, 5, main] isAlive = true name = main isDaemon = false
Thread [blueln, 10, system] isAlive = true name = blueln isDaemon = true
Thread [blueln, 10, system] isAlive = true name = blueln isDaemon = true
Thread [blueln, 10, system] isAlive = true name = blueln isDaemon = true
Thread [blueln, 10, system] isAlive = true name = blueln isDaemon = true
Thread [blueln, 10, main] isAlive = true name = blueln isDaemon = false
Replace the comments, and the infinite loop is stopped to display the output at a specific time point. The output of the Job class is displayed.
[Java]
Hello Thread !!
Thread [Thread-0, 5, main] isAlive = true name = Thread-0 isDaemon = false
Thread [Thread-1, 5, main] isAlive = true name = Thread-1 isDaemon = false
Thread [Thread-2, 5, main] isAlive = true name = Thread-2 isDaemon = false
Thread [blueln, 10, main] isAlive = true name = blueln isDaemon = false
Thread [blueln, 10, main] isAlive = true name = blueln isDaemon = false
Thread [blueln, 10, main] isAlive = true name = blueln isDaemon = false
T # = 0 Sun Nov 20 16:05:10 CST 2011
T # = 1 Sun Nov 20 16:05:10 CST 2011
T # = 2 Sun Nov 20 16:05:10 CST 2011
T # = 2 Sun Nov 20 16:05:18 CST 2011
T # = 1 Sun Nov 20 16:05:18 CST 2011
T # = 0 Sun Nov 20 16:05:19 CST 2011
T # = 2 Sun Nov 20 16:05:23 CST 2011
T # = 1 Sun Nov 20 16:05:23 CST 2011
T # = 0 Sun Nov 20 16:05:24 CST 2011
T # = 0 Sun Nov 20 16:05:27 CST 2011
T # = 0 Sun Nov 20 16:05:28 CST 2011
T # = 1 Sun Nov 20 16:05:30 CST 2011
T # = 2 Sun Nov 20 16:05:32 CST 2011
T # = 2 Sun Nov 20 16:05:33 CST 2011
T # = 1 Sun Nov 20 16:05:37 CST 2011
T # = 0 Sun Nov 20 16:05:37 CST 2011