• 线程池


    线程池指的就是将多个线程封装到一起进行操作。
    生活场景:功能模块开发,10人团队,3天完成。10人的团队就是线程池。
    可能会有以下几种情景:
    1、工作量大,有多少人要多少人,直至项目完成 // 无限量
    2、工作量大,必须要求10人 // 定长
    3、工作量大,只要求1人 // 单线程
     
    普通的执行线程池:java.util.concurrent.ExecutorService;
    调度线程池:java.util.concurrent.ScheduledExecutorService;
     
    线程池的创建可以利用 java.util.concurrent.Executors 类来完成,有如下几个方法:
    1.创建无大小限制的线程池:public static ExecutorService newCachedThreadPool()
    2.创建固定大小的线程池:public static ExecutorService newFixedThreadPool(int nThreads)
    3.单线程池:public static ScheduledExecutorService newSingleThreadScheduledExecutor()
    4.创建线程定时调度池:public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
     1 /***
     2  * 线程池 
     3  *
     4  */
     5 public class Test06 {
     6     public static void main(String[] args) {
     7 //        getNewSingleThreadExecutor();
     8 //        getNewFixedThreadPool();
     9         getNewScheduledThreadPool();
    10     }
    11     
    12     //无限制大小的线程池
    13     public static void getNewCachedThreadPool(){
    14         //现在创建了一个线程池模型,但是里面现在没有线程 (无大小限制的)
    15         ExecutorService executorService = Executors.newCachedThreadPool();
    16         for (int i = 0; i < 10; i++) {
    17             int x = i;
    18             //如果,在这里不等待,会创建好多线程,线程等待后,只会创建一个线程。
    19 //            try {
    20 //                Thread.sleep(1000);
    21 //            } catch (InterruptedException e) {
    22 //                e.printStackTrace();
    23 //            }
    24             executorService.submit(() ->{
    25                System.out.println(Thread.currentThread().getName() + "、x = " + x); 
    26             });
    27         }
    28         //关闭线程池
    29         executorService.shutdown();
    30     }
    31     
    32     
    33     public static void getNewSingleThreadExecutor(){
    34         //创建单线程池
    35         ExecutorService executorService = Executors.newSingleThreadExecutor();
    36         for (int i = 0; i < 10; i++) {
    37             int x = i;
    38             executorService.submit(() ->{
    39                System.out.println(Thread.currentThread().getName() + "、x = " + x); 
    40             });
    41         }
    42         executorService.shutdown();
    43     }
    44     
    45     public static void getNewFixedThreadPool(){
    46         //创建固定大小单线程池
    47         ExecutorService executorService = Executors.newFixedThreadPool(3);
    48         for (int i = 0; i < 10; i++) {
    49             int x = i;
    50             executorService.submit(() ->{
    51                 System.out.println(Thread.currentThread().getName() + "、x = " + x); 
    52             });
    53         }
    54         executorService.shutdown();
    55     }
    56     
    57     
    58     
    59     public static void getNewScheduledThreadPool(){
    60         //创建具有一个线程大小的定时调度线程池
    61         ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
    62         for (int i = 0; i < 10; i++) {
    63             int x = i;
    64             /***
    65              * 调度方法
    66              * 参数(方法,多长时间后开始,多长时间循环一次,时间单位)
    67              */
    68             executorService.scheduleAtFixedRate(() ->{
    69                 System.out.println(Thread.currentThread().getName() + "、x = " + x); 
    70             }, 3, 2, TimeUnit.SECONDS);
    71         }
    72         //现在得调度方法时3秒后执行,没两秒循环一次,会永远的执行下去,所以此处不能关闭。
    73         //executorService.shutdown();
    74     }
  • 相关阅读:
    Word常用定义的变量
    Delphi对Word一些进阶操作
    Delphi 统计Word文档中的字数
    Delphi对Word的基本操作
    Spring3 MVC请求参数获取的几种方法
    Spring3系列13-Controller和@RequestMapping
    Spring3系列12-Spring AOP AspectJ
    Spring3系列11-Spring AOP——自动创建Proxy
    Spring3系列10-Spring AOP——Pointcut,Advisor拦截指定方法
    Spring3系列9-Spring AOP——Advice
  • 原文地址:https://www.cnblogs.com/xiaoqisfzh/p/7355235.html
Copyright © 2020-2023  润新知