java架构《并发线程高级篇一》

  本章主要记录讲解并发线程的线程池。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   

 

    

posted @ 2017-05-15 16:58  我就是个小菜鸡  阅读(172)  评论(0编辑  收藏  举报