package com.bjpowernode.test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class PoolTest {
public static void main(String[] args) {
ExecutorService pool=Executors.newFixedThreadPool(5);//创建一个固定大小为5的线程池
for(int i=0;i<7;i++){
pool.submit(new MyThread());
}
pool.shutdown();
}
}
class MyThread extends Thread{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"正在执行。。。");
}
}
pool-1-thread-1正在执行。。。 pool-1-thread-3正在执行。。。 pool-1-thread-2正在执行。。。 pool-1-thread-4正在执行。。。 pool-1-thread-4正在执行。。。 pool-1-thread-5正在执行。。。 pool-1-thread-1正在执行。。。
public class PoolTest {
public static void main(String[] args) {
ExecutorService pool=Executors.newSingleThreadExecutor();//创建一个单线程池
for(int i=0;i<7;i++){
pool.submit(new MyThread());
}
pool.shutdown();
}
}
pool-1-thread-1正在执行。。。 pool-1-thread-1正在执行。。。 pool-1-thread-1正在执行。。。 pool-1-thread-1正在执行。。。 pool-1-thread-1正在执行。。。 pool-1-thread-1正在执行。。。 pool-1-thread-1正在执行。。。
public class PoolTest {
public static void main(String[] args) {
ExecutorService pool=Executors.newCachedThreadPool();
for(int i=0;i<5;i++){
pool.submit(new MyThread());
}
pool.shutdown();
}
}
pool-1-thread-1正在执行。。。 pool-1-thread-3正在执行。。。 pool-1-thread-2正在执行。。。 pool-1-thread-4正在执行。。。 pool-1-thread-5正在执行。。。
public class PoolTest {
public static void main(String[] args) {
ScheduledExecutorService pool=Executors.newScheduledThreadPool(6);
for(int i=0;i<4;i++){
pool.submit(new MyThread());
}
pool.schedule(new MyThread(), 1000, TimeUnit.MILLISECONDS);
pool.schedule(new MyThread(), 1000, TimeUnit.MILLISECONDS);
pool.shutdown();
}
}
pool-1-thread-1正在执行。。。 pool-1-thread-3正在执行。。。 pool-1-thread-2正在执行。。。 pool-1-thread-4正在执行。。。 pool-1-thread-6正在执行。。。 pool-1-thread-1正在执行。。。
public class PoolTest {
public static void main(String[] args) {
ScheduledExecutorService pool=Executors.newSingleThreadScheduledExecutor();
for(int i=0;i<4;i++){
pool.submit(new MyThread());
}
pool.schedule(new MyThread(), 1000, TimeUnit.MILLISECONDS);
pool.schedule(new MyThread(), 1000, TimeUnit.MILLISECONDS);
pool.shutdown();
}
}
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有