packagecom.itbac.thread;importjava.util.HashSet;importjava.util.Set;import java.util.concurrent.*;importjava.util.stream.Stream;/*** ThreadPoolExecutor 的 beforeExecute() 和 afterExecute()方法,

* 不是继承自 AbstractExecutorService , 这是设计上的一个败笔。

* 例如 netty 就是去实现 AbstractExecutorService的

*

* 线程池问题:如何获取线程池ThreadPoolExecutor正在运行的线程?*/

public classThreadPoolExecutorThreadQuestion {public static void main(String[] args) throwsInterruptedException {//main 线程启动子线程,子线程的创造来自于 Executors.defaultThreadFactory()

ExecutorService executorService=Executors.newCachedThreadPool();//定义线程容器,通过java的引用类型记录数据。

Set threadsContainer = new HashSet<>();//自定义的方法

setThreadFactory(executorService, threadsContainer);for (int i = 0; i < 5; i++) {

executorService.submit(()->{

});

}//线程池等待执行 3 ms

executorService.awaitTermination(3, TimeUnit.MILLISECONDS);

threadsContainer.stream().//过滤调不存活

filter(Thread::isAlive).

forEach(thread-> System.out.println("方法1:线程池的线程:" +thread)

);//方法二:

Thread mainThread =Thread.currentThread();

ThreadGroup mainThreadThreadGroup=mainThread.getThreadGroup();//获取线程组中的线程。

int count =mainThreadThreadGroup.activeCount();

System.out.println("count:"+count);

Thread[] threads= newThread[count];//enumerate 枚举,recurse 递归

mainThreadThreadGroup.enumerate(threads, true);

Stream.of(threads).filter(Thread::isAlive).forEach(thread-> System.out.println("方法2:线程池的线程:" +thread ));//关闭线程池

executorService.shutdown();/*** 输出结果:

方法1:线程池的线程:Thread[pool-1-thread-3,5,main]

方法1:线程池的线程:Thread[pool-1-thread-1,5,main]

方法1:线程池的线程:Thread[pool-1-thread-4,5,main]

方法1:线程池的线程:Thread[pool-1-thread-5,5,main]

方法1:线程池的线程:Thread[pool-1-thread-2,5,main]

count:7

方法2:线程池的线程:Thread[main,5,main] 主线程

方法2:线程池的线程:Thread[Monitor Ctrl-Break,5,main] 控制中断监视器

方法2:线程池的线程:Thread[pool-1-thread-1,5,main]

方法2:线程池的线程:Thread[pool-1-thread-2,5,main]

方法2:线程池的线程:Thread[pool-1-thread-3,5,main]

方法2:线程池的线程:Thread[pool-1-thread-4,5,main]

方法2:线程池的线程:Thread[pool-1-thread-5,5,main]*/}private static void setThreadFactory(ExecutorService executorService,SetthreadsContainer){if (executorService instanceofThreadPoolExecutor) {

ThreadPoolExecutor threadPoolExecutor=(ThreadPoolExecutor) executorService;//获取线程工厂

ThreadFactory oldThreadFactory =threadPoolExecutor.getThreadFactory();//在把线程工程设置到包装类 DelegatingThreadFactory ,再设置回线程池。

threadPoolExecutor.setThreadFactory(newMyThreadFactory(oldThreadFactory,threadsContainer));

}

}//我的的线程工厂

private static class MyThreadFactory implementsThreadFactory {private finalThreadFactory threadFactory;private final SetthreadsContainer;private MyThreadFactory(ThreadFactory threadFactory, SetthreadsContainer) {this.threadFactory =threadFactory;this.threadsContainer =threadsContainer;

}

@OverridepublicThread newThread(Runnable r) {

Thread thread=threadFactory.newThread(r);//cache thread 记录线程

threadsContainer.add(thread);//删除不存活的线程//threadsContainer.removeIf(next -> !next.isAlive());

returnthread;

}

}

}

GitHub 加速计划 / th / ThreadPool
7
0
下载
A simple C++11 Thread Pool implementation
最近提交(Master分支:3 个月前 )
9a42ec13 - 11 年前
fcc91415 - 11 年前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐