Java线程池学习笔记

线程池

线程池三大方法,七大技术

池化技术:

程序的运行,本质就是占用系统的资源。我们应优化系统的使用,于是诞生了池化技术。

池化技术:事先准备好一些资源,有线程 需要就拿走,用完再还回去。

线程池的好处:

1.降低资源的消耗

2.提高相应的速度

3.方便管理

即线程复用,可以控制最大并发数,管理线程

线程池三大方法


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//Executor 工具类 ,三大方法,不推荐直接创建,会浪费资源
public class Demo09 {
    public static void main (String[] args) {
        ExecutorService service = Executors.newSingleThreadExecutor();//单个线程
//        Executors.newFixedThreadPool(5);//创建固定大小的线程
//        Executors.newCachedThreadPool();//可伸缩线程
        try {
            for (int i = 0; i < 5; i++) {
                service.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"进来了");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //程序结束,需要关闭线程池
            service.shutdown();
        }

    }
}

线程池七大参数

源码分析:

    public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,//核心和最大都是1
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
    public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,//核心默认为0,最大的为21亿
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }

通过分析可以看出,以上的三个方法全部调用了ThreadPoolExecutor 方法。

  public ThreadPoolExecutor(int corePoolSize,//核心线程池大小
                              int maximumPoolSize,//最大核心线程池大小
                              long keepAliveTime,//当一个可被回收的线程的空闲时间大于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.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

通过ThreadPoolExecuto方法自定义线程池
package Mao.com.JUC;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

//自己手动创造一个线程池
public class Demo10 {
    public static void main (String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                1,//核心线程数
                3,//最大线程数
                3,//空闲线程存活最大时间
                TimeUnit.SECONDS,//时间单位
                new LinkedBlockingQueue<>(3),//阻塞队列及其大小。即等待的线程
                Executors.defaultThreadFactory(),//默认的线程工厂
                new ThreadPoolExecutor.AbortPolicy()//线程拒绝策略,即超过线程池最大承载的拒绝策略
        );
    }
}

corePoolSize:核心线程数

线程池维护的最小线程数量,核心线程创建后不会被回收(注意:设置allowCoreThreadTimeout=true后,空闲的核心线程超过存活时间也会被回收)。

大于核心线程数的线程,在空闲时间超过keepAliveTime后会被回收。

线程池刚创建时,里面没有一个线程,当调用 execute() 方法添加一个任务时,如果正在运行的线程数量小于corePoolSize,则马上创建新线程并运行这个任务。

maximumPoolSize:最大线程数

线程池允许创建的最大线程数量。

当添加一个任务时,核心线程数已满,线程池还没达到最大线程数,并且没有空闲线程工作队列已满的情况下,创建一个新线程并执行。即当任务的数量>核心线程数+阻塞队列数时,就会开启新线程。

keepAliveTime:空闲线程存活时间

当一个可被回收的线程的空闲时间大于keepAliveTime,就会被回收。

可被回收的线程:

  1. 设置allowCoreThreadTimeout=true的核心线程。
  2. 大于核心线程数的线程(非核心线程)。
unit:时间单位

keepAliveTime的时间单位:

TimeUnit.NANOSECONDS
TimeUnit.MICROSECONDS
TimeUnit.MILLISECONDS // 毫秒
TimeUnit.SECONDS//秒
TimeUnit.MINUTES
TimeUnit.HOURS
TimeUnit.DAYS
workQueue:工作队列

存放待执行任务的队列:当提交的任务数超过核心线程数大小后,再提交的任务就存放在工作队列,任务调度时再从队列中取出任务。它仅仅用来存放被execute()方法提交的Runnable任务。工作队列实现了BlockingQueue接口。

JDK默认的工作队列有五种:

ArrayBlockingQueue 数组型阻塞队列:数组结构,初始化时传入大小,有界,FIFO,使用一个重入锁,默认使用非公平锁,入队和出队共用一个锁,互斥。
LinkedBlockingQueue 链表型阻塞队列:链表结构,默认初始化大小为Integer.MAX_VALUE,有界(近似无界),FIFO,使用两个重入锁分别控制元素的入队和出队,用Condition进行线程间的唤醒和等待。
SynchronousQueue 同步队列:容量为0,添加任务必须等待取出任务,这个队列相当于通道,不存储元素。
PriorityBlockingQueue 优先阻塞队列:无界,默认采用元素自然顺序升序排列。
DelayQueue 延时队列:无界,元素有过期时间,过期的元素才能被取出。

threadFactory:线程工厂

创建线程的工厂,可以设定线程名、线程编号等。

handler:四种拒绝策略

// 1 AbortPolicy 默认的拒绝策略
//如果线程超过线程池最大容量,抛出异常,最大容量=MAX阻塞队列+MAX线程池

    public static class AbortPolicy implements RejectedExecutionHandler {
        /**
         * Creates an {@code AbortPolicy}.
         */
        public AbortPolicy() { }

        /**
         * Always throws RejectedExecutionException.
         *
         * @param r the runnable task requested to be executed
         * @param e the executor attempting to execute this task
         * @throws RejectedExecutionException always
         */
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            throw new RejectedExecutionException("Task " + r.toString() +
                                                 " rejected from " +
                                                 e.toString());
        }
    }

示例:
    public static void main (String[] args) throws Exception{
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1,3,3,TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

        for (int i = 0; i < 7; i++) {
            threadPoolExecutor.execute(()->{
                System.out.println(Thread.currentThread().getName()+"正在执行");
            });
        }
    }
threadPoolExecutor.shutdown();
output:
    Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task Mao.com.JUC

2.CallerRunsPolicy 调用线程处理该任务。

哪里来的去哪里 ,即哪个线程调用的就去哪里

public static class CallerRunsPolicy implements RejectedExecutionHandler {
        /**
         * Creates a {@code CallerRunsPolicy}.
         */
        public CallerRunsPolicy() { }

        /**
         * Executes task r in the caller's thread, unless the executor
         * has been shut down, in which case the task is discarded.
         *
         * @param r the runnable task requested to be executed
         * @param e the executor attempting to execute this task
         */
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            if (!e.isShutdown()) {
                r.run();
            }
        }
    }

示例:
        public static void main (String[] args) throws Exception{
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1,3,3,TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());

        for (int i = 0; i < 7; i++) {
            threadPoolExecutor.execute(()->{
                System.out.println(Thread.currentThread().getName()+"正在执行");
            });
        }
        threadPoolExecutor.shutdown();
    }

output:
pool-1-thread-1正在执行
main正在执行//注意,这里没有报错,而是把线程又抛给了主线程
pool-1-thread-1正在执行
pool-1-thread-1正在执行
pool-1-thread-1正在执行
pool-1-thread-3正在执行
pool-1-thread-2正在执行

3.DiscardPolicy:丢弃任务,但是不抛出异常。可能导致无法发现系统的异常状态

    public static void main (String[] args) throws Exception{
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1,3,3,TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardPolicy());

        for (int i = 0; i < 7; i++) {
            threadPoolExecutor.execute(()->{
                System.out.println(Thread.currentThread().getName()+"正在执行");
            });
        }
        threadPoolExecutor.shutdown();
    }

output:
pool-1-thread-1正在执行
pool-1-thread-2正在执行
pool-1-thread-3正在执行
pool-1-thread-2正在执行
pool-1-thread-1正在执行
pool-1-thread-3正在执行


4.DiscardOldestPolicy:丢弃队列最前面的任务,然后重新提交被拒绝的任务

这里的线程会尝试请求最老的线程,如果最老的线程处于空闲,则替换最老的线程

    public static void main (String[] args) throws Exception{
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1,3,3,TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy());

        for (int i = 0; i < 7; i++) {
            threadPoolExecutor.execute(()->{
                System.out.println(Thread.currentThread().getName()+"正在执行");
            });
            Thread.sleep(10);
        }
        threadPoolExecutor.shutdown();
    }

output:
pool-1-thread-1正在执行
pool-1-thread-1正在执行
pool-1-thread-1正在执行
pool-1-thread-1正在执行
pool-1-thread-1正在执行
pool-1-thread-1正在执行
pool-1-thread-1正在执行
    //注意,这里执行了七条线程,因为主线程每次会休息10ms,所以当最后一条线程进入时,最老的线程处于空闲,则丢弃

线程池的执行流程

——来自网络

img

最大线程数如何定义?

1.CPU密集型:几核就定义为几

Runtime.getRuntime().availableProcessors();//动态获取CPU的核数

2,IO密集型:判断你的程序中十分消耗IO的线程,线程大于这个数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黑mmd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值