码迷,mamicode.com
首页 > 编程语言 > 详细

线程池

时间:2017-08-13 23:17:32      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:rate   操作   .exe   nts   single   java   core   多少   stat   

线程池指的就是将多个线程封装到一起进行操作。
生活场景:功能模块开发,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     }

 

线程池

标签:rate   操作   .exe   nts   single   java   core   多少   stat   

原文地址:http://www.cnblogs.com/xiaoqisfzh/p/7355235.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!