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
- ThreadGroup和ExecutorService
- ThreadGroup和ExecutorService
- 0094 ThreadGroup和ExecutorService比较【进阶】
- ThreadGroup其实比ExecutorService更好
- ThreadGroup与ExecutorService的使用
- ThreadGroup其实比ExecutorService更好
- ThreadGroup其实比ExecutorService更好
- ThreadGroup其实比ExecutorService更好
- Thread,ThreadGroup和Runnable
- 自定义ThreadFactory和ThreadGroup
- ThreadGroup
- ThreadGroup
- Executor/ExecutorService和ExecutorCompletionService
- ExecutorService和Executors
- Executors 和 ExecutorService
- Executor和ExecutorService
- Executor/ExecutorService和ExecutorCompletionService
- 多线程 之 线程组(ThreadGroup)和线程组的中断
- android基础知识点
- 利用ArcGIS去除Landsat 7 ETM+影像条带
- VMware 安装虚拟机 (centos6.5)并搭建rails环境
- window2008 64位系统没有office组件问题分析及解决
- 安装Drupal过程中的问题及解决方法
- ThreadGroup和ExecutorService
- Linux下MySQL数据库常用基本操作
- scanner使用中遇见的问题
- jquery的ajax使用
- use of address of unaligned structure member 转
- 利用js的prototype属性给数组去重
- JsonConfig的常见的方法
- Android 显示原理简介
- SRP:单一职责原则