java基础——多线程(线程池)

一、线程池

概念原理理解:

首先介绍在Tcp服务器编程模型的原理,每一个客户端连接用一个单独的线程为之服务,当与客户端的会话结束时,线程也就结束了,即每来一个客户端连接,服务器端就要创建一个新线程。这好比假设每个报名学员都要通过我来亲自接待,以便给每个学员一种好的感觉,但每个学员报名手续要花费半个小时,对于50名同学,我一个个接待和为之办理手续,显然不实际,我会怎么做呢?我会先接待每一个学员,打完招呼后,再把他分配给一名工作人员去办理手续,这样,我就接待了每名学员。
如果访问服务器的客户端很多,那么服务器要不断地创建和销毁线程,这将严重影响服务器的性能。如果真的来一名学员,我们都安排一名新工作人员为之服务,也是不可能的,那公司岂不是要招聘很多工作人员?而是应该一名工作人员服务完一名学员,空闲下来后,一旦有新的学员要服务,我又立即安排该工作人员为新学员服务。线程池的概念与此类似,首先创建一些线程,它们的集合称为线程池,当服务器接受到一个客户请求后,就从线程池中取出一个空闲的线程为之服务,服务完后不关闭该线程,而是将该线程还回到线程池中。

在线程池的编程模式下,任务是提交给整个线程池,而不是直接交给某个线程,线程池在拿到任务后,它就在内部找有无空闲的线程,再把任务交给内部某个空闲的线程,这就是封装。记住,任务是提交给整个线程池,一个线程同时只能执行一个任务,但可以同时向一个线程池提交多个任务。


1、线程池的概念与Executors类的应用

1)创建固定大小的线程池
2)创建缓存线程池

3)创建单一线程池(如何实现线程死掉后重新启动?)

[java]  view plain copy
  1. import java.util.concurrent.ExecutorService;  
  2. import java.util.concurrent.Executors;  
  3.   
  4. public class ThreadPoolTest {  
  5.     public static void main(String[] args) {  
  6.         /*1、固定线程池 
  7.         // 创建一个线程池,每次只执行固定的3个线程任务 
  8.         ExecutorService pool = Executors.newFixedThreadPool(3);*/     
  9.           
  10.         /*2、缓存线程池,内部执行线程的任务不定,实现线程死掉后重新启动*/  
  11.          //ExecutorService pool = Executors.newCachedThreadPool();  
  12.            
  13.          /*3、创建单一线程池*/  
  14.          ExecutorService pool = Executors.newSingleThreadExecutor();  
  15.           
  16.         //向pool池提交了10个任务,有十个Runnable  
  17.         for (int i = 1; i <= 3; i++) {  
  18.             final int task = i;  
  19.             pool.execute(new Runnable() {  
  20.                 @Override  
  21.                 public void run() {  
  22.                     //每个任务执行10次循环  
  23.                     for (int j = 1; j <= 5; j++) {  
  24.                         try {  
  25.                             Thread.sleep(20);  
  26.                         } catch (InterruptedException e) {  
  27.                             e.printStackTrace();  
  28.                         }  
  29.                         //线程x,正在循环第j次 ,在第i个任务里  
  30.                         System.out.println(Thread.currentThread().getName()  
  31.                                 + " loop of  " + j+"  for task of  "+task);  
  32.                     }  
  33.                 }  
  34.             });  
  35.         }  
  36.         //执行完所有的任务,结束线程池  
  37.         pool.shutdown();  
  38.           
  39.         //只要程序执行到这,立即结束线程  
  40.         //pool.shutdownNow();  
  41.     }  
  42. }  
打印:

1)创建固定大小的线程池

pool-1-thread-3 loop of  1  for task of  3
pool-1-thread-2 loop of  1  for task of  2
pool-1-thread-1 loop of  1  for task of  1
pool-1-thread-3 loop of  2  for task of  3
pool-1-thread-2 loop of  2  for task of  2
pool-1-thread-1 loop of  2  for task of  1
pool-1-thread-1 loop of  3  for task of  1
pool-1-thread-2 loop of  3  for task of  2
pool-1-thread-3 loop of  3  for task of  3
2)创建缓存线程池

pool-1-thread-1 loop of  1  for task of  1
pool-1-thread-2 loop of  1  for task of  2
pool-1-thread-3 loop of  1  for task of  3
pool-1-thread-2 loop of  2  for task of  2
pool-1-thread-1 loop of  2  for task of  1
pool-1-thread-3 loop of  2  for task of  3
pool-1-thread-1 loop of  3  for task of  1
pool-1-thread-2 loop of  3  for task of  2
pool-1-thread-3 loop of  3  for task of  3

3)创建单一线程池

pool-1-thread-1 loop of  1  for task of  1
pool-1-thread-1 loop of  2  for task of  1
pool-1-thread-1 loop of  3  for task of  1
pool-1-thread-1 loop of  1  for task of  2
pool-1-thread-1 loop of  2  for task of  2
pool-1-thread-1 loop of  3  for task of  2
pool-1-thread-1 loop of  1  for task of  3
pool-1-thread-1 loop of  2  for task of  3
pool-1-thread-1 loop of  3  for task of  3

2、关闭线程池
shutdown与shutdownNow的比较
3|用线程池启动定时器
1)调用ScheduledExecutorService的schedule方法,返回的ScheduleFuture对象可以取消任务。

2)支持间隔重复任务的定时方式,不直接支持绝对定时方式,需要转换成相对时间方式。

[java]  view plain copy
  1.     //3、用线程池启动定时器  
  2.     ScheduledExecutorService scheduledService = Executors.newScheduledThreadPool(1);  
  3.     scheduledService.scheduleAtFixedRate(  
  4.             new Runnable(){  
  5.                 public void run() {  
  6.                     System.out.println("bomb!!!");  
  7.                 }},  //实例一个Runable对象  
  8.             5// 起始延迟 时间  
  9.             1,//每隔一秒爆炸一次  
  10.             TimeUnit.SECONDS);//秒为单位  
  11.   
  12. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值