Java线程池

一.CacheThreadPool

  1. CacheThreadPool线程池特点:

    • 无核心线程

    • 线程数量无上限

    • 空闲线程在60秒后,将会被终止删除

  2. 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);
                         }
                     }
                 });
             }
         }

  3. 输出结果:

    image-20230919203031556

二.FixedThreadPool

  1. FixedThreadPool线程池特点:

    • 可以自己设置核心线程数量

    • 其中也在只有核心线程

    • 线程的最大值为int的最大值

    • 空闲线程不会终止删除

  2. 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);
                         }
                     }
                 });
             }
         }

  3. 运行结果

    image-20230919205607338

三.ScheduledThreadPool

  1. ScheduledThreadPool线程池的特点

    • 可以设置核心线程数量

    • 可设置延迟运行命令或者定期执行命令

    • 既有核心线程,又有非核心线程

    • 非核心线程在空闲时立即就会被回收

    • 线程数量的最大值为int的最大值

  2. 延迟执行代码演示(延迟五秒后执行 )

     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);
             }

  3. 周期性执行代码演示

    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);

  4. scheduleWithFixedDelay和scheduleAtFixedDelay的区别:

    • scheduleWithFixedDelay:以固定的延时执行,delay(延时)指的是一次执行终止和下一次执行开始之间的延迟。

    • scheduleAtFixedRate:以固定的时间间隔执行,interval(时间间隔)指的是两次执行开始之间的时间间隔。

四.SingleThreadExecutor

  1. SingleThreadExecutor线程池的特点

    • 只有一个工作线程

    • 任务的执行顺序根据指定顺序进行

    • 线程空闲后不会终止线程

  2. 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);
                         }
                     }
                 });
             }
         }

  3. 运行结果

    image-20230919211709455

四 .ThreadPoolExecutor

  1. ThreadPoolExecutor线程池特点

    • 可以根据自己的业务常见创建所需要的线程

  2. ThreadPoolExecutor线程池实现CacheThreadPool线程池

            
     new ThreadPoolExecutor(0,Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue<>(),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());

  3. ThreadPoolExecutor线程池实现FixedThreadPool线程池

             
    new ThreadPoolExecutor(2,2,0L,TimeUnit.SECONDS,new LinkedBlockingDeque<>(),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());

  4. ScheduledThreadPoolExecutor线程池实现ScheduledThreadPool线程池

    ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(2);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值