线程池的取值(一)与拒绝策略

public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)

先估算一个并发数,(pv/时间段=qps,qps*单线程响应时间=并发数=总qps/单线程qps)即为corePoolSize,*1.2为maximumPoolSize
workQueue长度=设计响应时间/单线程响应时间*corePoolSize - corePoolSize
假设每个线程0.1s,可以容忍2s内给客户端返回,
线程池经qps验算确定为10个线程,0.1秒过10个线程,2s过20*10=200,容量可取200-10,最多2s队列末尾的线程也可以获得执行并返回给客户端

参考:https://www.cnblogs.com/waytobestcoder/p/5323130.html

================================================================================
jvm内存有限,不可能容纳无限线程,否则内存爆掉,我们需要一定的拒绝策略,首先要拒绝,其次又要保留一定的信息,以便日后分析作出调整

private static class MyRejectedExecutionHandler implements RejectedExecutionHandler {

private static Logger LOGGER = LoggerFactory.getLogger(MyRejectedExecutionHandler.class);

@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
LOGGER.error("thread discard");
}
}

注意,如果是callable get方式,这种情况会导致get无限阻塞,注意设置超时限制

==================================================================

https://www.cnblogs.com/longzhaoyu/p/4539341.html这篇文章比较有意思,里面的拒绝策略:
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    if (!executor.isShutdown()) {
                        executor.execute(r);
                    }
                }

stack over flow

posted on 2018-07-19 23:50  silyvin  阅读(552)  评论(0编辑  收藏  举报