zoukankan      html  css  js  c++  java
  • Java线程池的四种用法与使用场景

    线程池的作用主要是为了提升系统的性能以及使用率。文章刚开始就提到,如果我们使用最简单的方式创建线程,如果用户量比较大,那么就会产生很多创建和销毁线程的动作,这会导致服务器在创建和销毁线程上消耗的性能可能要比处理实际业务花费的时间和性能更多。

    作者:小涛来源:程序员的成长|2019-10-25 10:35

     一、如下方式存在的问题

    1. new Thread() { 
    2.     @Override 
    3.     public void run() { 
    4.         // 业务逻辑 
    5.     } 
    6. }.start(); 

    1、首先频繁的创建、销毁对象是一个很消耗性能的事情;2、如果用户量比较大,导致占用过多的资源,可能会导致我们的服务由于资源不足而宕机;3、综上所述,在实际的开发中,这种操作其实是不可取的一种方式。

    二、使用线程池有什么优点

    1、线程池中线程的使用率提升,减少对象的创建、销毁;2、线程池可以控制线程数,有效的提升服务器的使用资源,避免由于资源不足而发生宕机等问题;

    三、线程池的四种使用方式

    1、newCachedThreadPool

    创建一个线程池,如果线程池中的线程数量过大,它可以有效的回收多余的线程,如果线程数不足,那么它可以创建新的线程。

    1. public static void method() throws Exception { 
    2.  
    3.     ExecutorService executor = Executors.newCachedThreadPool(); 
    4.  
    5.     for (int i = 0; i < 5; i++) { 
    6.  
    7.         final int index = i; 
    8.  
    9.         Thread.sleep(1000); 
    10.  
    11.         executor.execute(new Runnable() { 
    12.             @Override 
    13.             public void run() { 
    14.                 System.out.println(Thread.currentThread().getName() + "  " + index); 
    15.             } 
    16.         }); 
    17.     } 

    执行结果

    通过分析我看可以看到,至始至终都由一个线程执行,实现了线程的复用,并没有创建多余的线程。如果当我们的业务需要一定的时间进行处理,那么将会出现什么结果。我们来模拟一下。

    可以明显的看出,现在就需要几条线程来交替执行。

    不足:这种方式虽然可以根据业务场景自动的扩展线程数来处理我们的业务,但是最多需要多少个线程同时处理缺是我们无法控制的;

    优点:如果当第二个任务开始,第一个任务已经执行结束,那么第二个任务会复用第一个任务创建的线程,并不会重新创建新的线程,提高了线程的复用率;

    2、newFixedThreadPool

    这种方式可以指定线程池中的线程数。举个栗子,如果一间澡堂子最大只能容纳20个人同时洗澡,那么后面来的人只能在外面排队等待。如果硬往里冲,那么只会出现一种情景,摩擦摩擦...

    首先测试一下最大容量为一个线程,那么会不会是我们预测的结果。

    1. public static void method_01() throws InterruptedException { 
    2.  
    3.     ExecutorService executor = Executors.newFixedThreadPool(1); 
    4.  
    5.     for (int i = 0; i < 10; i++) { 
    6.  
    7.         Thread.sleep(1000); 
    8.         final int index = i; 
    9.  
    10.         executor.execute(() -> { 
    11.             try { 
    12.                 Thread.sleep(2 * 1000); 
    13.             } catch (InterruptedException e) { 
    14.                 e.printStackTrace(); 
    15.             } 
    16.             System.out.println(Thread.currentThread().getName() + "  " + index); 
    17.         }); 
    18.     } 
    19.     executor.shutdown(); 

    执行结果

    我们改为3条线程再来看下结果

    优点:两个结果综合说明,newFixedThreadPool的线程数是可以进行控制的,因此我们可以通过控制最大线程来使我们的服务器打到最大的使用率,同事又可以保证及时流量突然增大也不会占用服务器过多的资源。

    3、newScheduledThreadPool

    该线程池支持定时,以及周期性的任务执行,我们可以延迟任务的执行时间,也可以设置一个周期性的时间让任务重复执行。 该线程池中有以下两种延迟的方法。

    • scheduleAtFixedRate

    测试一

    1. public static void method_02() { 
    2.     ScheduledExecutorService executor = Executors.newScheduledThreadPool(5); 
    3.  
    4.     executor.scheduleAtFixedRate(new Runnable() { 
    5.         @Override 
    6.         public void run() { 
    7.             long start = new Date().getTime(); 
    8.             System.out.println("scheduleAtFixedRate 开始执行时间:" + 
    9.                     DateFormat.getTimeInstance().format(new Date())); 
    10.             try { 
    11.                 Thread.sleep(5000); 
    12.             } catch (InterruptedException e) { 
    13.                 e.printStackTrace(); 
    14.             } 
    15.             long end = new Date().getTime(); 
    16.             System.out.println("scheduleAtFixedRate 执行花费时间=" + (end - start) / 1000 + "m"); 
    17.             System.out.println("scheduleAtFixedRate 执行完成时间:" + DateFormat.getTimeInstance().format(new Date())); 
    18.             System.out.println("======================================"); 
    19.         } 
    20.     }, 1, 5, TimeUnit.SECONDS); 

    执行结果

    测试二

    总结:以上两种方式不同的地方是任务的执行时间,如果间隔时间大于任务的执行时间,任务不受执行时间的影响。如果间隔时间小于任务的执行时间,那么任务执行结束之后,会立马执行,至此间隔时间就会被打乱。

    • scheduleWithFixedDelay

    测试一

    1. public static void method_03() { 
    2.     ScheduledExecutorService executor = Executors.newScheduledThreadPool(2); 
    3.  
    4.     executor.scheduleWithFixedDelay(new Runnable() { 
    5.         @Override 
    6.         public void run() { 
    7.             long start = new Date().getTime(); 
    8.             System.out.println("scheduleWithFixedDelay 开始执行时间:" + 
    9.                     DateFormat.getTimeInstance().format(new Date())); 
    10.             try { 
    11.                 Thread.sleep(1000); 
    12.             } catch (InterruptedException e) { 
    13.                 e.printStackTrace(); 
    14.             } 
    15.             long end = new Date().getTime(); 
    16.             System.out.println("scheduleWithFixedDelay执行花费时间=" + (end - start) / 1000 + "m"); 
    17.             System.out.println("scheduleWithFixedDelay执行完成时间:" 
    18.                     + DateFormat.getTimeInstance().format(new Date())); 
    19.             System.out.println("======================================"); 
    20.         } 
    21.     }, 1, 2, TimeUnit.SECONDS); 

    执行结果

    测试二

    1. public static void method_03() { 
    2.     ScheduledExecutorService executor = Executors.newScheduledThreadPool(2); 
    3.  
    4.     executor.scheduleWithFixedDelay(new Runnable() { 
    5.         @Override 
    6.         public void run() { 
    7.             long start = new Date().getTime(); 
    8.             System.out.println("scheduleWithFixedDelay 开始执行时间:" + 
    9.                     DateFormat.getTimeInstance().format(new Date())); 
    10.             try { 
    11.                 Thread.sleep(5000); 
    12.             } catch (InterruptedException e) { 
    13.                 e.printStackTrace(); 
    14.             } 
    15.             long end = new Date().getTime(); 
    16.             System.out.println("scheduleWithFixedDelay执行花费时间=" + (end - start) / 1000 + "m"); 
    17.             System.out.println("scheduleWithFixedDelay执行完成时间:" 
    18.                     + DateFormat.getTimeInstance().format(new Date())); 
    19.             System.out.println("======================================"); 
    20.         } 
    21.     }, 1, 2, TimeUnit.SECONDS); 

    执行结果

    总结:同样的,跟scheduleWithFixedDelay测试方法一样,可以测出scheduleWithFixedDelay的间隔时间不会受任务执行时间长短的影响。

    4、newSingleThreadExecutor

    这是一个单线程池,至始至终都由一个线程来执行。

    1. public static void method_04() { 
    2.  
    3.     ExecutorService executor = Executors.newSingleThreadExecutor(); 
    4.  
    5.     for (int i = 0; i < 5; i++) { 
    6.         final int index = i; 
    7.         executor.execute(() -> { 
    8.             try { 
    9.                 Thread.sleep(2 * 1000); 
    10.             } catch (InterruptedException e) { 
    11.                 e.printStackTrace(); 
    12.             } 
    13.             System.out.println(Thread.currentThread().getName() + "   " + index); 
    14.         }); 
    15.     } 
    16.     executor.shutdown(); 

    执行结果

    四、线程池的作用

    线程池的作用主要是为了提升系统的性能以及使用率。文章刚开始就提到,如果我们使用最简单的方式创建线程,如果用户量比较大,那么就会产生很多创建和销毁线程的动作,这会导致服务器在创建和销毁线程上消耗的性能可能要比处理实际业务花费的时间和性能更多。线程池就是为了解决这种这种问题而出现的。

    同样思想的设计还有很多,比如数据库连接池,由于频繁的连接数据库,然而创建连接是一个很消耗性能的事情,所有数据库连接池就出现了。

  • 相关阅读:
    DB2基本语句操作
    makefile简析与例子
    io多路复用epoll归纳
    Selenium 3----设置元素等待
    Selenium 3----获取断言信息
    Selenium 3----鼠标、键盘操作常用方法
    Selenium 3----WebDriver常用方法
    算法时间复杂度求解
    Selenium 3----控制浏览器操作
    操作系统基础知识学习
  • 原文地址:https://www.cnblogs.com/telwanggs/p/12372166.html
Copyright © 2011-2022 走看看