首页 技术 正文
技术 2022年11月6日
0 收藏 865 点赞 1,027 浏览 4764 个字

  本章主要记录讲解并发线程的线程池。java.util.concurrent工具包里面的工具类。

一:Executor框架:

    Executors创建线程池的方法:

      

newFixedThreadPool()方法: 该方法返回一个固定数量的线程池,该方法的线程数始终不变,当有一个任务提交时,若线程池中空闲,则立即执行,若没有,则会被暂缓在一个任务队列中等待有空闲的线程去执行。newSingleThreadExecutor()方法:创建一个线程的线程池,若空闲则执行。若没有,则暂缓在任务队列中。newCachedThreadPool()方法:返回一个可根据实际情况调整线程个数的线程池,不限制最大线程数量,若用空闲的线程则执行任务,若无任务则不创建线程,并且每一个空闲线程会在60秒后自动回收。newScheduledThreadPool()方法:该方法返回一个SchededExecutorService对象,但该线程池可以指定线程的数量。newFixedThreadPool使用心得:因为该方法创建的是一个固定的线程池,底层运用的队列是linkBlockingQueue队列,该队列是阻塞无序队列,所以切记,当首次任务列队完成调度后,后面的顺序就会被打乱,排序则需要根据指定的规则排序。newCachedThreadExecutor使用心得:创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。newSingleThreadExecutor使用心得:创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。newScheduledThreadPool使用心得:创建一个定长线程池,支持定时及周期性任务执行。

     

    代码解析:

       

 1  //Task内部类:  
2
3         class Task extends Thread{
4
5         private int TaskId;
6
7         public Task(Integer TaskId){
8         this.TaskId=TaskId;
9         }
10           @Override
11           public void run() {
12             long start = System.currentTimeMillis();
13             try {
14               System.out.println("Run-----" +"TaskId:"+TaskId);
15               //模仿处理数据需要5s
16               Thread.sleep(5000);
17               long end = System.currentTimeMillis() - start;
18               System.out.println(" 执行时间:" + end);
19               } catch (InterruptedException e) {
20                 // TODO Auto-generated catch block
21                 e.printStackTrace();
22               }
23
24               }
25           }
26
27

      

 1 // newFixedThreadPool方法测试:
2
3             
4
5           /**
6           * newFixedThreadPool线程池测试 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。队列无序
7           * @throws InterruptedException
8           */
9           private static void FixedThredPoolTest() throws InterruptedException {
10             System.out.println("newFixedThreadPool线程池测试-------------------");
11             //创建固定的10个线程的线程池 队列阻塞无序
12             ExecutorService pool=Executors.newFixedThreadPool(10);
13             /**
14             * 这里产生15个任务 看是否一次性处理10个任务,
15             * 然后5个任务阻塞放在LinkBlockingQueue队列里面,
16             * 然后等10个任务处理完在执行
17             */
18               for (int i = 1; i <= 15; i++) {
19                 // 执行给定的命令 执行任务
20                 pool.execute(new Task(i));
21                 }
22
23                 //关闭启动线程
24                 pool.shutdown();
25                 // 等待子线程结束,再继续执行下面的代码
26                 pool.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
27                 System.out.println("all thread complete");
28               }
29
30         

      

 1  // newCachedThreadPool方法测试:
2
3             
4
5            /**
6            * newCachedThreadPool线程池测试 创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
7            * @throws InterruptedException
8            */
9            private static void CachedThreadPoolTest() throws InterruptedException {
10               System.out.println("newCachedThreadPool线程池测试-------------------");
11               ExecutorService pool=Executors.newCachedThreadPool();
12
13                 for (int i =1; i <= 10; i++) {
14                   // 执行给定的命令
15                   pool.execute(new Task(i));
16                 }
17                 //关闭启动线程
18                 pool.shutdown();
19                 // 等待子线程结束,再继续执行下面的代码
20                 pool.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
21                 System.out.println("all thread complete");
22               }
23
24

       

 1 // newSingleThreadExecutor方法测试:
2
3         
4
5             /**
6
7             * newSingleThreadExecutor测试:创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
8             * @throws InterruptedException
9             */
10             private static void SingleThreadPoolTest() throws InterruptedException {
11               System.out.println("newSingleThreadExecutor线程池测试-------------------");
12               ExecutorService pool=Executors.newSingleThreadExecutor();
13                 for (int i = 1; i <= 5; i++) {
14                   // 执行给定的命令
15                   pool.execute(new Task(i));
16                   }
17                 //关闭启动线程
18                 pool.shutdown();
19                 // 等待子线程结束,再继续执行下面的代码
20                 pool.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
21                 System.out.println("all thread complete");
22               }
23
24

      

 1  // newScheduledThreadPool方法测试:
2
3           
4
5           /**
6           * newScheduledThreadPool测试 创建一个定长线程池,支持定时及周期性任务执行。
7           */
8           private static void ScheduledThreadPoolTest() {
9             System.out.println("newScheduledThreadPool线程池测试-------------------");
10             ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
11             /* 参数:
12             command - 要执行的任务
13             initialdelay - 首次执行的延迟时间
14             delay - 一次执行终止和下一次执行开始之间的延迟
15             unit - initialdelay 和 delay 参数的时间单位*/
16             ScheduledFuture<?> scheduleTask = scheduler.scheduleWithFixedDelay(new Task(1), 5, 1, TimeUnit.SECONDS);
17           }
18
19   

 

    

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901