Works Java thread pool

                                                                                  Interpretation of the thread pool source

As more and more cpu audit, the inevitable use of multi-threading technology to take advantage of its computing power. So, multi-threading technology is server-side developers have to master the technology.

Thread creation and destruction, are related to system calls, more consumption of system resources, so the introduction of the thread pool, to avoid frequent thread creation and destruction.

In Java with a Executors utility class, you can create a thread pool for us, its essence is a new ThreadPoolExecutor object. The thread pool is almost a compulsory interview questions. This section combined with the source code, to talk about the principle of ThreadExecutor

First, create a thread pool

ThreadPoolExecutor most complete look at the parameters of the constructor:

 

①corePoolSize: core number of threads the thread pool, it means, even if the thread pool without any task, there will be corePoolSize threads in waiting for the other tasks.

②maximumPoolSize: maximum number of threads, no matter how many tasks you commit, the thread pool maximum number of worker threads is maximumPoolSize.

③keepAliveTime: survival time thread. When the number of threads in the thread pool greater than corePoolSize, if keepAliveTime waited a long time has not yet perform the task, the thread exits.

⑤unit: This is used to specify keepAliveTime units such as seconds: TimeUnit.SECONDS.

⑥workQueue: a blocking queue, job submission will be placed in the queue.

⑦threadFactory: thread factory used to create a thread, the main thread is to give a name, the name of the default thread factory: pool-1-thread-3.

⑧handler: denial strategy, a thread when the thread pool is exhausted, and the queue was full when invoked.

These are used when creating a thread pool parameters, there is often interview the interviewer asked this question.

Second, the thread pool implementation process

Here will be described using FIG perform a process thread pool

 

The task is to submit to the thread pool, will first determine whether the current number of threads less than corePoolSize, if less than create a thread to perform tasks submitted, otherwise the mission will put workQueue queue, if workQueue is full, it is determined whether the current thread is less than the number of maximumPoolSize, If less than the thread executing the task is created, otherwise it will call handler, to indicate that the thread pool refused to accept the task.

Jdk1.8.0_111 here with source code, for example, look at the implementation.

1, look at the method of the thread pool executor

 

①: determine the current number of active threads is less than corePoolSize, if less than, the calling thread is created to perform tasks addWorker

②: if not less than corePoolSize, then the task is added to workQueue queue.

③: If you put workQueue fails, create a thread to perform the task, if the time failed to create thread (not less than the current number of threads maximumPoolSize), will call reject (internal call handler) refused to accept the task.

2, look at the method implemented addWorker

 

When you create a piece of code that is non-core thread, that core is equal to false. Determine whether the current number of threads greater than or equal maximumPoolSize, if greater than or equal false if that situation comes on top of the thread failure ③ created.

addWorker method of the lower half:

 

① Create a Worker object, but also instantiate a Thread object.

② start start this thread

3, and then look at its implementation in Worker

 

You can see when you create threadFactory Worker calls to create a thread. ② top of the thread will start a run method is triggered Worker thread calls.

4, we look at the next logical method runWorker

 

Thread calls runWoker, called in a loop getTask method to read from workerQueue task in while, and then perform the task. As long as getTask method does not return null, this thread will not quit.

5, the last way to achieve the look getTask

 

① we first matter allowCoreThreadTimeOut, this variable default value is false. wc> corePoolSize is determined whether the current number of threads is greater than corePoolSize.

②如果当前线程数大于corePoolSize,则会调用workQueue的poll方法获取任务,超时时间是keepAliveTime。如果超过keepAliveTime时长,poll返回了null,上边提到的while循序就会退出,线程也就执行完了。

如果当前线程数小于corePoolSize,则会调用workQueue的take方法阻塞在当前。

 

                                                                        线程池是什么时候创建的

 

带着几个问题进入源码分析:

1. 线程池是什么时候创建线程的?

2. 任务runnable task是先放到core到maxThread之间的线程,还是先放到队列?

3. 队列中的任务是什么时候取出来的?

4. 什么时候会触发reject策略?

5. core到maxThread之间的线程什么时候会die?

6. task抛出异常,线程池中这个work thread还能运行其他任务吗?

 

先写一段基础代码,进入分析

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

public static void main(String[] args) {

 

        ExecutorService executorService = new ThreadPoolExecutor(250, TimeUnit.DAYS,

                new ArrayBlockingQueue<>(1), new ThreadFactory() {

            @Override

            public Thread newThread(Runnable r) {

                Thread thread = new Thread(r);

//                thread.setDaemon(true);

                return thread;

            }

        });

        // 对象创建后,线程实际还没开始创建

 

        // 执行execute时,检查当前池中线程数大小是否小于core number, 如果是,则创建新线程

        executorService.execute(() -> {

            System.out.println("任务1@" + Thread.currentThread().getName());

            sleepTime();

            System.out.println(1);

        });

 

        //检查当前池中线程数大小是否小于core number, 如果是,则创建新线程

        executorService.execute(() -> {

            System.out.println("任务2@" + Thread.currentThread().getName());

            sleepTime();

            System.out.println(2);

        });

 

        // 检查当前池中线程数大小是否小于core number, 如果不是,则偿试放入队列

        // 这个任务是加到队列去的, 注意队列大小只有1,

        // TODO 队列中的任务是什么时候取出来的?   任务1或者2结束后所占用的线程 会运行队列中的任务,这个任务是在最后才运行,比4运行的还晚

        executorService.execute(() -> {

            System.out.println("任务3@" + Thread.currentThread().getName());

            sleepTime();

            System.out.println(3);

        });

 

        // 检查当前池中线程数大小是否小于core number, 如果不是,则偿试放入队列,放入队列也失败,则增加新的worker线程

        // 这个任务是加到core以外的新线程去的

        executorService.execute(() -> {

            System.out.println("任务4@" + Thread.currentThread().getName());

            sleepTime();

            System.out.println(4);

        });

    }

  

注意第3行,创建一个核心池2, 最大池5, 队列为1的线程池

至少在new ThreadPoolExecutor()时,Thread对象并没有初始化. 这里仅仅指定了几个初始参数

 

执行第一个execute时,进入调试jdk源码

 代码块1

第一个if, 判断如果当前线程数小于corePoolSize, 则创建新的核心worker对象(Worker中指向Thread对象,保持引用,保证不会被GC回收)

     我们的示例代码中,第1和第2个任务都是这样创建出线程的

第二个if,   判断如果当前线程数大于corePoolSize, 并偿试放入队列 workQueue.offer(command) , 放入成功后等待线程池调度【见后面的getTask()】

     示例代码中,第3个任务是这样等待调度的,最后才执行

第三个if,  偿试放入队列 workQueue.offer(command) 失败, 增加一个非core的线程

     示例代码中,第4个任务是这样开始的

 

 

然后再看addWorker()的过程

 

new Worker构造和线程启动

线程启动后,又做了哪些工作:

异常分析

没抛异常时,此线程会一直在while(task !=null || (task = getTask())!=null)中跑

那么有异常时,再看一下processWorkerExit

 

可以 看出,有异常时 旧的worker会被删除(GC回收),再创建新的Worker, 即有异常时 旧worker不可能再执行新的任务

 

总结:

Q. 线程池是什么时候创建线程的?

A.任务提交的时候

Q.任务runnable task是先放到core到maxThread之间的线程,还是先放到队列?

A.先放队列!!!

Q. 队列中的任务是什么时候取出来的?

A. worker中 runWorker() 一个任务完成后,会取下一个任务

Q. 什么时候会触发reject策略?

A.队列满并且maxthread也满了, 还有新任务,默认策略是reject

Q. core到maxThread之间的线程什么时候会die?

A.  没有任务时,或者抛异常时。   

   core线程也会die的,core到maxThread之间的线程有可能会晋升到core线程区间,

   core max只是个计数,线程并不是创建后就固定在一个区间了

Q. task抛出异常,线程池中这个work thread还能运行其他任务吗?

A. 不能。 但是会创建新的线程, 新线程可以运行其他task。

对于 schedulerThreadPoolExecutor?   虽然有新线程,但是旧的循环任务不会再继续执行了, 开发实践推荐任务中捕获所有Exception

 

                                                                               如何合理配置线程数量

1)CPU密集型:

       定义:CPU密集型的意思就是该任务需要大量运算,而没有阻塞,CPU一直全速运行。
       CPU密集型任务只有在真正的多核CPU上才可能得到加速(通过多线程)。
       CPU密集型任务配置尽可能少的线程数。
       CPU密集型线程数配置公式:CPU核数+1个线程的线程池

2)IO密集型:

       定义:IO密集型,即该任务需要大量的IO,即大量的阻塞。
       在单线程上运行IO密集型任务会导致浪费大量的CPU运算能力浪费在等待。
       所以IO密集型任务中使用多线程可以大大的加速程序运行,即使在单核CPU上,这种加速主要利用了被浪费掉的阻塞时间。
      
       第一种配置方式:
       由于IO密集型任务线程并不是一直在执行任务,则应配置尽可能多的线程。
       配置公式:CPU核数 * 2。
       第二种配置方式:
       IO密集型时,大部分线程都阻塞,故需要多配置线程数。
       配置公式:CPU核数 / 1 – 阻塞系数(0.8~0.9之间)
       比如:8核 / (1 – 0.9) = 80个线程数
 

 

参考:https://www.cnblogs.com/yszzu/p/10122658.html

https://www.cnblogs.com/qingquanzi/p/8146638.html

https://blog.csdn.net/lixinkuan328/article/details/94501073

发布了740 篇原创文章 · 获赞 65 · 访问量 10万+

Guess you like

Origin blog.csdn.net/qq_41723615/article/details/104372959