ThreadGroup和ExecutorService

来源:互联网 发布:软件测试相关书籍 编辑:程序博客网 时间:2024/06/04 19:46

ExecutorService:

package BackStage;import java.util.concurrent.Executors;import java.util.concurrent.ExecutorService;public class JavaThreadPool {   public static void main(String[] args) {       // 创建一个可重用固定线程数的线程池        ExecutorService pool = Executors.newFixedThreadPool(2);       // 创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口       Thread t1 = new MyThread();       Thread t2 = new MyThread();       Thread t3 = new MyThread();       Thread t4 = new MyThread();       Thread t5 = new MyThread();       // 将线程放入池中进行执行       pool.execute(t1);       pool.execute(t2);       pool.execute(t3);       pool.execute(t4);       pool.execute(t5);       // 关闭线程池       pool.shutdown();   }}class MyThread extends Thread {   @Override   public void run() {       System.out.println(Thread.currentThread().getName() + "正在执行。。。");   }}

ThreadGroup:
class MyThread extends Thread {boolean stopped;MyThread(ThreadGroup tg, String name) {super(tg, name);stopped = false;}public void run() {System.out.println(Thread.currentThread().getName() + " starting.");try {for (int i= 1; i <1000; i++)  System.out.print("."); Thread.sleep(250); synchronized (this) {   if (stopped)     break;   } }} catch (Exception exc) {System.out.println(Thread.currentThread().getName() + " interrupted.");}System.out.println(Thread.currentThread().getName() + " exiting.");}synchronized void myStop() {stopped = true;}}public class Main {public staticvoid main(String args[]) throws Exception {ThreadGroup tg = new ThreadGroup("My Group");MyThread thrd = new MyThread(tg,"MyThread #1");MyThread thrd2 = new MyThread(tg,"MyThread #2");MyThread thrd3 = new MyThread(tg,"MyThread #3");thrd.start();thrd2.start();thrd3.start();Thread.sleep(1000);System.out.println(tg.activeCount() +" threads in thread group.");Thread thrds[] = new Thread[tg.activeCount()];tg.enumerate(thrds);for (Thread t : thrds)System.out.println(t.getName());thrd.myStop();Thread.sleep(1000);System.out.println(tg.activeCount() +" threads in tg.");tg.interrupt();}}

由以上的代码可以看出:ThreadGroup比ExecutorService多以下几个优势  

1.ThreadGroup可以遍历线程,知道那些线程已经运行完毕,那些还在运行  

2.可以通过ThreadGroup.activeCount知道有多少线程从而可以控制插入的线程数

 

 

 

 

 

0 0
原创粉丝点击