一.CacheThreadPool
-
CacheThreadPool线程池特点:
-
无核心线程
-
线程数量无上限
-
空闲线程在60秒后,将会被终止删除
-
-
CacheThreadPool线程池应用
public static void main(String[] args) { ExecutorService pool = Executors.newCachedThreadPool(); System.out.println("当前时间:"+ LocalTime.now()); for (int i = 0; i < 10; i++) { int num = i ; pool.execute(new Runnable() { @Override public void run() { System.out.println("当前时间"+LocalTime.now()+",线程:"+Thread.currentThread().getName()+",序号:"+num); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } }); } }
-
输出结果:
二.FixedThreadPool
-
FixedThreadPool线程池特点:
-
可以自己设置核心线程数量
-
其中也在只有核心线程
-
线程的最大值为int的最大值
-
空闲线程不会终止删除
-
-
FixedThreadPool线程池的应用
public class FixedThreadPoolDemo { /* FixedThreadPool(可重用固定个数的线程池,只有核心线程,且不会被回收,核心线程数可设置) */ public static void main(String[] args) { // 设置核心线程数为3个 ExecutorService pool = Executors.newFixedThreadPool(3); // 输出当前时间 System.out.println("当前时间:"+ LocalTime.now()); // 设置有任务需要被执行十次 for (int i = 0; i < 10; i++) { int num = i; pool.execute(new Runnable() { @Override public void run() { System.out.println("当前时间:"+LocalTime.now()+",线程:"+Thread.currentThread().getName()+",序号:"+num); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } }); } }
-
运行结果
三.ScheduledThreadPool
-
ScheduledThreadPool线程池的特点
-
可以设置核心线程数量
-
可设置延迟运行命令或者定期执行命令
-
既有核心线程,又有非核心线程
-
非核心线程在空闲时立即就会被回收
-
线程数量的最大值为int的最大值
-
-
延迟执行代码演示(延迟五秒后执行 )
ScheduledExecutorService pool = Executors.newScheduledThreadPool(2); System.out.println("当前时间为:"+ LocalTime.now()); for (int i = 0; i < 3; i++) { int num = i; pool.schedule(new Runnable() { @Override public void run() { System.out.println("当前时间:"+LocalTime.now()+",线程:"+Thread.currentThread().getName()+",序号:"+num); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } },5, TimeUnit.SECONDS); }
-
周期性执行代码演示
1.scheduleAtFixedRate()方法(开始后两秒执行,每一个线程之间各三秒)
ScheduledExecutorService pool = Executors.newScheduledThreadPool(2); System.out.println("当前时间为:"+LocalTime.now()); pool.scheduleAtFixedDelay(new Runnable() { @Override public void run() { System.out.println("当前时间:"+LocalTime.now()+",线程:"+Thread.currentThread().getName()); try { Thread.sleep(5000); } catch (InterruptedException e) { throw new RuntimeException(e); } } },2,3,TimeUnit.SECONDS);
2.scheduleWithFixedRate()方法(开始后两秒执行,每一个线程之间各三秒)
ScheduledExecutorService pool = Executors.newScheduledThreadPool(2); System.out.println("当前时间为:"+LocalTime.now()); pool.scheduleWithFixedDelay(new Runnable() { @Override public void run() { System.out.println("当前时间:"+LocalTime.now()+",线程:"+Thread.currentThread().getName()); try { Thread.sleep(5000); } catch (InterruptedException e) { throw new RuntimeException(e); } } },2,3,TimeUnit.SECONDS);
-
scheduleWithFixedDelay和scheduleAtFixedDelay的区别:
-
scheduleWithFixedDelay
:以固定的延时执行,delay(延时)指的是一次执行终止和下一次执行开始之间的延迟。 -
scheduleAtFixedRate
:以固定的时间间隔执行,interval(时间间隔)指的是两次执行开始之间的时间间隔。
-
四.SingleThreadExecutor
-
SingleThreadExecutor线程池的特点
-
只有一个工作线程
-
任务的执行顺序根据指定顺序进行
-
线程空闲后不会终止线程
-
-
SingleThreadExecutor线程池的应用
public static void main(String[] args) { ExecutorService pool = Executors.newSingleThreadExecutor(); System.out.println("当前时间:"+ LocalTime.now()); for (int i = 0; i < 7; i++) { int num = i; pool.execute(new Runnable() { @Override public void run() { System.out.println("当前时间:"+LocalTime.now()+",线程:"+Thread.currentThread().getName()+",序号:"+num); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } }); } }
-
运行结果
四 .ThreadPoolExecutor
-
ThreadPoolExecutor线程池特点
-
可以根据自己的业务常见创建所需要的线程
-
-
ThreadPoolExecutor线程池实现CacheThreadPool线程池
new ThreadPoolExecutor(0,Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue<>(),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
-
ThreadPoolExecutor线程池实现FixedThreadPool线程池
new ThreadPoolExecutor(2,2,0L,TimeUnit.SECONDS,new LinkedBlockingDeque<>(),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
-
ScheduledThreadPoolExecutor线程池实现ScheduledThreadPool线程池
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(2);