线程池介绍

线程池介绍

线程池:三大方法、7大参数
池化技术:

程序的运行,本质:占用系统的资源! 优化资源的使用!=>池化技术
线程池、连接池、内存池、对象池… 创建、销毁。十分浪费资源
池化技术:事先准备好一些资源,有人要用,就来我这里拿,用完之后还 给我。

线程池的好处:
1、降低资源的消耗
2、提高响应的速度
3、方便管理。
线程复用、可以控制最大并发数、管理线程

一般来说我们都是使用的自定义线程池,如下:
请添加图片描述

三大方法


 ExecutorService threadPool = Executors.newSingleThreadExecutor();//单个线程

 ExecutorService threadPool1  = Executors.newFixedThreadPool(5);//创建一个固定大小的线程池

 ExecutorService threadPool2 = Executors.newCachedThreadPool();//弹性

七大参数()

咋们点进这三大方法中去探探源码

== newSingleThreadExecutor() ==

    public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

== newFixedThreadPool(5)==

    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }

== newCachedThreadPool()==

    public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }

运用

try{
            for (int i = 0; i < 50; i++) {
                threadPool2.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"----->OK");
                });
            }

        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }
        
自定义线程池(常用)

我们发现他们都是使用ThreadPoolExcutor来创建的
那我们点进去看一下这个类的方法

    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), defaultHandler);
    }

这里看到非常多的参数,这里就是我们这一部分要将的七大参数了

public ThreadPoolExecutor(int corePoolSize, // 核心线程池大小 
						int maximumPoolSize, // 最大核心线程池大小 
						long keepAliveTime, // 超时了没有人调用就会释放
						TimeUnit unit, // 超时单位
						BlockingQueue<Runnable> workQueue, // 阻塞队列 
						ThreadFactory threadFactory, // 线程工厂:创建线程的,一般 不用动
						RejectedExecutionHandler handle // 拒绝策略) 
				{
						this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), defaultHandler);
    }

这就是七个参数所代表的意义了,咋们在源码中也可以看到请添加图片描述

请添加图片描述

四大拒绝策略


new ThreadPoolExecutor.AbortPolicy() // 满了,还有资源进来,不处理,抛出异常
new ThreadPoolExecutor.CallerRunsPolicy() // 哪来的去哪里!main方法来的就会main方法去执行 
new ThreadPoolExecutor.DiscardPolicy() //队列满了,丢掉任务,不会抛出异常! 
new ThreadPoolExecutor.DiscardOldestPolicy() //队列满了,尝试去和最早的竞争,也不会 抛出异常! 

这里是源码中的定义
请添加图片描述

那么看到这儿了,我们应该怎么自定义一个线程池呢?

// Executors 工具类、3大方法 


 ExecutorService threadPool = new ThreadPoolExecutor(
                2,
                5,
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );

        try{
            for (int i = 0; i < 15; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"-------->OK");
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值