Coder Social home page Coder Social logo

Comments (4)

jonesun avatar jonesun commented on September 14, 2024

hello

from mybloggitalk.

huarenshen avatar huarenshen commented on September 14, 2024

线程池参数 核心线程数量corePoolSize、队列存储任务数量queueCapacity、最大线程数量maxPoolSize设置技巧

corePoolSize设置需要考虑大概率情况下任务来了刚好可以直接被处理
假设每个任务平均需要tasktime秒处理,则每个线程每钞可处理1/tasktime个任务。系统每秒有tasks个任务需要处理,则需要的线程数为:tasks/(1/tasktime),即taskstasktime个线程数。
假设系统每秒任务数为100~1000,每个任务耗时0.1秒,则需要100
0.1至10000.1,即10~100个线程。那么corePoolSize应该设置为大于10小于100,具体数字最好根据8020原则,即80%情况下系统每秒任务数。
若系统80%的情况下每秒任务数小于200,最多时为1000,则corePoolSize可设置为200
0.1即20个。

queueCapacity设置主要根据任务的最大响应时间
假设最大能接受响应时间为2秒,忽略任务处理时间计算可知队列长度为 (corePoolSize/tasktime)*responsetime 即为 (20/0.1)*2等于400

maxPoolSize需要考虑到最大响应时间的队列长度情况下每秒需要处理最多的任务数量,计算可知 (最大tasks-queueCapacity)*tasktime = maxPoolSize (1000-400)*0.1=60
以上关于线程数量的计算并没有考虑CPU的情况。若结合CPU的情况,比如,当线程数量达到50时,CPU达到100%,则将maxPoolSize设置为60也不合适,此时若系统负载长时间维持在每秒1000个任务,
则超出线程池处理能力,应根据合适的逻辑设法降低每个任务的处理时间(tasktime)。
参考链接 https://blog.csdn.net/chang_ge/article/details/89412626

from mybloggitalk.

zhengyiyi1995 avatar zhengyiyi1995 commented on September 14, 2024

线程池(ThreadPoolExecutor)中队列,池大小,核心线程的关系
1:核心线程数量(corePoolSize):线程池中能否允许同时并发运行的线程的数量
2:线程池大小(maximumPoolSize):线程池中最多能够容纳的线程的最大数量。
3:队列(workQueue):对超过了核心线程数量任务是多余任务的等待队列队列。
如果队列发过来的任务,发现线程池中正在运行的线程的数量小于核心线程,则立即创建新的线程,无需进入队列等待。如果正在运行的线程等于或者大于核心线程,则多余的任务就会提交到阻塞队列中去。
如果阻塞队列满了就会在不超出当前线程池大小的线程中使用新线程继续工作,超出线城池大小的任务则会被拒绝.

SynchronousQueue(直接递交):相当于阻塞队列容量为0
ArrayBlockingQueue(有界队列):在初始化的时候会定义当前阻塞队列数量的大小,new ArrayBlockingQueue<>(阻塞队列大小);
LinkedBlockingQuene、LinkedBlockingDeque(无界队列):该阻塞队列也可以设置大小new LinkedBlockingQuene<>(阻塞队列大小); 当不设置的时候默认大小为Integer.MAX_VALUE = 0x7fffffff;(7fffffff(十六进制) = 2147483647(十进制))

https://blog.csdn.net/weixin_42476141/article/details/106211463?utm_medium=distribute.pc_relevant_bbs_down.none-task-blog-baidujs-1.nonecase&depth_1-utm_source=distribute.pc_relevant_bbs_down.none-task-blog-baidujs-1.nonecase

from mybloggitalk.

jorkey-wu avatar jorkey-wu commented on September 14, 2024

maximumPoolSize:线程池中允许创建的最大线程数,线程池中的当前线程数目不会超过该值。

也就是说,maximumPoolSize >= 核心线程数 + 非核心线程数。

即:如果队列中任务已满,并且当前线程个数(poolSize)小于maximumPoolSize,那么会创建新的线程来执行任务,如果当前线程个数(poolSize)等于maximumPoolSize,则会拒绝任务。

参考链接:https://www.cnblogs.com/winner-0715/p/7388662.html

from mybloggitalk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.