Java-线程池

线程池:三大方法、7大参数、4种拒绝策略

池化技术

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

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

线程线程池:三大方法

public static void main(String[] args) {
        //单例池
        //ExecutorService threadPool = Executors.newSingleThreadExecutor();
        //固定线程池,创建一个固定的线程池大小
        //ExecutorService threadPool = Executors.newFixedThreadPool(5);
        //缓冲线程池,可伸缩,遇强则强
        ExecutorService threadPool = Executors.newCachedThreadPool();
        try {
            for (int i = 0; i < 10; i++) {
                //使用线程池来创建线程
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"=> ok");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭线程池
            threadPool.shutdown();
        }
    }

线程池本质—ThreadPoolExecutor

    public ThreadPoolExecutor(int corePoolSize,	//核心线程大小
                              int maximumPoolSize,	//最大核心线程数
                              long keepAliveTime,	//超时了没人调用就会释放
                              TimeUnit unit,	    //超时单位
                              BlockingQueue<Runnable> workQueue,	//阻塞队列
                              ThreadFactory threadFactory,	//线程工厂,创建线程的,一般不要动
                              RejectedExecutionHandler handler	//拒绝策略) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.acc = System.getSecurityManager() == null ?
                null :
                AccessController.getContext();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

自定义线程池以及线程池的拒绝策略

 /**
     * new ThreadPoolExecutor.AbortPolicy()  //队列满了,丢弃任务并抛出RejectedExecutionException异常
     * new ThreadPoolExecutor.CallerRunsPolicy() //由调用线程处理该任务
     * new ThreadPoolExecutor.DiscardOldestPolicy() //队列满了,不会抛出异常
     * new ThreadPoolExecutor.DiscardPolicy() //队列满了,丢弃队列最前面的任务,然后重新提交被拒绝的任务,不会抛出异常
     *
     */
    public void pool(){
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2,
                5,
                3,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy()
        );

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值