线程池学习笔记 (初识线程池...)

线程池:
   ? 为什么使用线程池
   1/ 线程缺乏统一管理,占用过多资源,
   2/ 缺乏更多的功能 ,如 定时执行 , 定期执行等
   --------------------------------------------
   使用线程池的优势:
   1/重用存在的线程,减少创建,消亡线程的开销
   2/有效控制最大并发数 , 提高系统资源使用率
   3/ 定时执行,定期执行等
   ------------------------
   线程池位于 java.util.concurrent包中
   顶级接口Executor ,真正线程池的接口ExecutorService
   java.util.concurrent.Executors类提供了创建线程池的方法
 --------------------------------------------------------------
 创建线程的方法;
           Executors.newCachedThreadPool();//创建一个可以缓存的线程池,有任务时才创建新任务
           Executors.newSingleThreadExecutor(); //创建单一线程
           Executors.newFixedThreadPool(3); // 创建固定个数线程的线程池
           Executors.newScheduledThreadPool(2); // 创建一个固定长度的线程池,而且可以延迟或者定时的方式执行任务*/
        -------------------------------------------------------------------------------------------------------------
           public class PoolTest {
               public static void main(String[] args) {
                   //创建线程池
                   ExecutorService executorService = Executors.newCachedThreadPool();
                   int i= 10;
                   while (i>0) {
                       i--;
                       executorService.execute(new MyRunable(10));
                       try {
                           Thread.sleep(100);  //每次执行任务后休眠100毫秒
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                   }
               }
           }
           class MyRunable implements Runnable {
               int s;
               public MyRunable(int s) {
                   this.s = s;
               }
               @Override
               public void run() {
                   System.out.println(Thread.currentThread().getName()+"========="+s);
               }
           }
        -------------------------------------------------------------------------------------------------------------
   结果:
                       pool-1-thread-1=========10
                       pool-1-thread-1=========10
                       pool-1-thread-1=========10
                       pool-1-thread-1=========10
                       pool-1-thread-1=========10
                       pool-1-thread-1=========10
                       pool-1-thread-1=========10
                       pool-1-thread-1=========10
                       pool-1-thread-1=========10
                       pool-1-thread-1=========10
        -------------------------------------------------------------------------------------------------------------
        当取消掉休眠后:  可以发现线程池的优势:   可以发现最高的时候有8个线程在执行,其中1,3 线程执行了两次
                       pool-1-thread-1=========10
                       pool-1-thread-3=========10
                       pool-1-thread-4=========10
                       pool-1-thread-1=========10
                       pool-1-thread-3=========10
                       pool-1-thread-5=========10
                       pool-1-thread-2=========10
                       pool-1-thread-6=========10
                       pool-1-thread-7=========10
                       pool-1-thread-8=========10


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值