ThreadUtil使API更加具体化,因而为线程编程新手提供了一定的方便性,从而简化了API。其源码如下:
- 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);
- }
- }
- 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();
- }
- }
- }
- }
输出结果:- 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
将注释部分替换,无限循环被停止,以显示出特定时刻的输出,即可看到Job类打印的结果
- 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