//线程池
ExecutorService pool = Executors.newCachedThreadPool();
//创建100个线程
for(int i = 0; i < count; i++){
MyThread target = new MyThread("para1", "para2", "para3", countDownLatch);
}
如何查看我的线程池里有多少线程?
不会,你这个线程池是无参的,使用的是默认线程工厂,想要使用自己的线程的话,需要把线程放到线程工厂中。
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
threadFactory);
}
默认的线程工厂是这个:
static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
也可以自己实现覆盖~
如果觉得回答对你有帮助的话,可采纳~~
不会,线程池内线程分为核心线程和临时线程,但是无论哪种线程均由线程池维护,但是可以在创建时指定线程工厂。线程池相关问题可以参考https://blog.csdn.net/yue_hu/article/details/116237645。另外获取线程池的线程可以通过getPoolSize获得活动的总线程量,getCorePoolSize活动活动的核心线程量。线程池常用方法可以参考https://blog.csdn.net/yue_hu/article/details/116295451