|
5 | 5 | public class ThreadPoolTest { |
6 | 6 |
|
7 | 7 | public static void main(String[] args) { |
8 | | - |
9 | | - customThreadPool(); |
10 | | - // cachePool(); |
11 | | - // fixedThreadPool(); |
12 | | - |
| 8 | + customThreadPool(); // 使用ThreadPoolExecutor 自定义线程池 |
| 9 | + // cachePool(); // 使用 Executors 创建默认线程池 |
| 10 | + // fixedThreadPool(); // 使用 Executors 创建默认线程池 |
13 | 11 | } |
14 | 12 |
|
15 | 13 | /** |
16 | | - * 自定义线程池 |
| 14 | + * 自定义线程池,ThreadPoolExecutor |
17 | 15 | */ |
18 | 16 | private static void customThreadPool() { |
19 | 17 |
|
20 | 18 | int corePoolSize = 2; // 主流线程个数 |
21 | 19 | int maximumPoolSize = 4; // 线程最大个数 |
22 | 20 | long keepAliveTime = 0L; // 大于主流线程个数的线程空闲的过期时间 wait for new tasks before terminating |
23 | 21 | TimeUnit unit = TimeUnit.MILLISECONDS; // 时间单元 |
24 | | - BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(); // 工作队列,有三种类SynchronousQueue、LinkedBlockingQueue(在所有 corePoolSize 线程都忙时新任务在队列中等待,maximumPoolSiz失效)、ArrayBlockingQueue,分别对应同步队列、无界队列、有界队列。 |
25 | | - |
26 | | - ThreadPoolExecutor executorPool = new ThreadPoolExecutor(corePoolSize, |
27 | | - maximumPoolSize, keepAliveTime, unit, workQueue); |
28 | | - |
29 | | - for(int i=0;i<20;i++){ |
30 | | - System.out.println("start i:"+i); |
31 | | - executorPool.execute(new Runnable(){ |
| 22 | + BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(1); // 工作队列,有三种类SynchronousQueue、LinkedBlockingQueue(在所有 corePoolSize 线程都忙时新任务在队列中等待)、ArrayBlockingQueue,分别对应同步队列、无界队列(Integer.MAX_VALUE)、有界队列。 |
| 23 | + ThreadFactory threadFactory = new ThreadFactory(){ // 自定义生成线程的名称 |
| 24 | + @Override |
| 25 | + public Thread newThread(Runnable r) { |
| 26 | + Thread myThread=new Thread(r); |
| 27 | + myThread.setName("my-"+Thread.currentThread().getName()); |
| 28 | + return myThread; |
| 29 | + } |
| 30 | + }; |
| 31 | + RejectedExecutionHandler handler =new ThreadPoolExecutor.CallerRunsPolicy(); // 队列满,且达到maximumPoolSize时,再添加线程到线程池的拒绝策略,有4种 ThreadPoolExecutor.AbortPolicy抛异常、ThreadPoolExecutor.DiscardPolicy 默默抛弃不处理、ThreadPoolExecutor.DiscardOldestPolicy 抛弃等待最久的、ThreadPoolExecutor.CallerRunsPolicy 由向线程池提交任务的线程来执行该任务 |
32 | 32 |
|
33 | | - @Override |
34 | | - public void run() { |
35 | | - try { |
36 | | - Thread.sleep(1000); |
37 | | - } catch (InterruptedException e) { |
| 33 | + // 构造线程池 |
| 34 | + ThreadPoolExecutor executorPool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,threadFactory,handler); |
| 35 | + for(int i=1;i<=20;i++){ // 并发最大任务数 = maximumPoolSize+队列深度 |
| 36 | + try { |
| 37 | + int j=i; |
| 38 | + executorPool.execute(new Runnable(){ |
| 39 | + @Override |
| 40 | + public void run() { |
| 41 | + try { |
| 42 | + System.out.println(j + " start at "+Thread.currentThread().getName() ); |
| 43 | + Thread.sleep(2000); |
| 44 | + System.out.println(j +" end at "+Thread.currentThread().getName()); |
| 45 | + } catch (InterruptedException e) { |
| 46 | + } |
38 | 47 | } |
39 | | - System.out.println(Thread.currentThread().getName() |
40 | | - + "正在执行。。。"); |
41 | | - } |
42 | | - }); |
43 | | - System.out.println("end i:"+i); |
| 48 | + }); |
| 49 | + System.out.println(i+" ok"); |
| 50 | + }catch(Exception e){ |
| 51 | + System.out.println(i+" fail:"+e.getMessage()); |
| 52 | + } |
44 | 53 | } |
45 | | - |
46 | 54 | executorPool.shutdown(); |
| 55 | + System.out.println("over"); |
47 | 56 | } |
48 | 57 |
|
49 | 58 | /** |
50 | | - * 使用固定数目的线程池<br> |
| 59 | + * 使用固定数目的线程池,Executors |
51 | 60 | * 使用LinkedBlockingQueue固定大小 |
52 | 61 | */ |
53 | 62 | private static void fixedThreadPool() { |
54 | 63 | ExecutorService executorPool = Executors.newFixedThreadPool(4); |
55 | 64 | for (int i = 0; i < 10; i++) { |
56 | | - executorPool.execute(new Runnable() { |
57 | | - |
58 | | - @Override |
59 | | - public void run() { |
| 65 | + executorPool.execute( () -> { |
60 | 66 | System.out.println(Thread.currentThread().getName() |
61 | 67 | + "正在执行。。。"); |
62 | | - } |
63 | | - |
64 | 68 | }); |
65 | 69 | } |
66 | 70 | executorPool.shutdown(); |
67 | 71 | } |
68 | 72 |
|
69 | 73 | /** |
70 | | - * 动态增减线程数目的线程池<br> |
| 74 | + * 动态增减线程数目的线程池,Executors |
71 | 75 | * 使用SynchronousQueue 实现线程实时增加 |
72 | 76 | */ |
73 | 77 | private static void cacheThreadPool() { |
|
0 commit comments