ThreadUtil in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.