理解线程池运行过程(ThreadPoolExecutor举例,打印线程池当前线程数、队列任务数、已完成数等信息)

 java  理解线程池运行过程(ThreadPoolExecutor举例,打印线程池当前线程数、队列任务数、已完成数等信息)已关闭评论
11月 292021
 

jdk提供了非常方便的方式来生成线程池, 如Executors.newxxxxxx的方式, 实现实际使用的都是以下ThreadPoolExecutor的方法:

/**
* Creates a new {@code ThreadPoolExecutor} with the given initial
* parameters.
*
* @param corePoolSize the number of threads to keep in the pool, even
* if they are idle, unless {@code allowCoreThreadTimeOut} is set
* @param maximumPoolSize the maximum number of threads to allow in the
* pool
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the {@code keepAliveTime} argument
* @param workQueue the queue to use for holding tasks before they are
* executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute} method.
* @param threadFactory the factory to use when the executor
* creates a new thread
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
* @throws IllegalArgumentException if one of the following holds:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue}
* or {@code threadFactory} or {@code handler} is null
*/

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

线程池的管理是这样一个过程:

首先创建线程池, 然后根据任务的数量逐步将线程增大到corePoolSize数量, 如果此时任有任务增加, 则放置到workQueue中, 直到workQueue爆满为止, 然后继续增加线程池中的线程数量,增加处理能力,最终达到maximumPoolSize。 如果此时还是有任务增加进来会怎样呢 ? 这就需要handler来处理了,或者丢弃新任务, 或者拒绝新任务,或者挤占已有任务等。在任务队列何线程池都饱和情况下,一旦有线程处于等待(任务处理完毕, 没有新任务增加)状态的时间超过keepAliveTime,则该线程终止, 也就是说池中的线程数量会足部降低, 直至为corePoolSize数量为止。 至于threadFactory,可以自己新增一个,设置线程的自定义名称, daemon状态,便于后续排查错误。

我们用一个测试程序来帮助理解线程池的运行过程:

public class ThreadPoolTest {

private static ExecutorService es = new ThreadPoolExecutor(50, 100, 0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(100000));

public static void main(String[] args) throws Exception {
for (int i = 0; i < 100000; i++) {
es.execute(() -> {
System.out.print(1);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});

}

ThreadPoolExecutor tpe = ((ThreadPoolExecutor) es);
while (true) {
System.out.println();
int queueSize = tpe.getQueue().size();
System.out.println(“当前排队任务数:” + queueSize);

int activeCount = tpe.getActiveCount();
System.out.println(“当前活动线程数:” + activeCount);

long completedTaskCount = tpe.getCompletedTaskCount();
System.out.println(“执行完成线程数:” + completedTaskCount);

long taskCount = tpe.getTaskCount();
System.out.println(“总线程数:” + taskCount);

Thread.sleep(3000);
}
}

}

 

程序每3秒打印一次线程池情况:

11111111111111111111111111111111111111111111111111
当前排队线程数:99950
当前活动线程数:50
执行完成线程数:0
总线程数:100000
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
当前排队线程数:99849
当前活动线程数:50
执行完成线程数:127
总线程数:100000
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
当前排队线程数:99700
当前活动线程数:50
执行完成线程数:250
总线程数:100000
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
当前排队线程数:99550
当前活动线程数:50
执行完成线程数:400
总线程数:100000
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
当前排队线程数:99400
当前活动线程数:50
执行完成线程数:550
总线程数:100000
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
当前排队线程数:99250
当前活动线程数:50
执行完成线程数:700
总线程数:100000

。。。。。。

可以看到当前活动的线程数永远都是50, 为什么就没有突破?怎么没到100呢? 大家可以思考下,相信看了上面的描述,很容易就能理解了,DONE!

 

tomcat 大并发报错 Maximum number of threads (200) created for connector with address null and port 8080

 tomcat  tomcat 大并发报错 Maximum number of threads (200) created for connector with address null and port 8080已关闭评论
5月 082013
 

1、INFO: Maximum number of threads (200) created for connector with address null and port 8091

说明:最大线程数错误

解决方案:

 

使用线程池,用较少的线程处理较多的访问,可以提高tomcat处理请求的能力。使用方式:

首先。打开/conf/server.xml,增加

 

  1. <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"      
  2.         maxThreads="500" minSpareThreads="20" maxIdleTime="60000" />    


最大线程500(一般服务器足以),最小空闲线程数20,线程最大空闲时间60秒。

然后,修改<Connector …>节点,增加executor属性,如:

  1. <Connector executor="tomcatThreadPool"      
  2.                port="80" protocol="HTTP/1.1"      
  3.                connectionTimeout="60000"    
  4.                keepAliveTimeout="15000"    
  5.                maxKeepAliveRequests="1"    
  6.                redirectPort="443"    
  7.                ……/>    


2、java.net.SocketException: Too many open files

 

当tomcat并发用户量大的时候,单个jvm进程确实可能打开过多的文件句柄。

使用 #lsof -p 10001|wc -l   查看文件操作数

如下操作:

  1. (1).ps -ef |grep tomcat  查看tomcat的进程ID,记录ID号,假设进程ID为10001     
  2. (2).lsof -p 10001|wc -l    查看当前进程id为10001的 文件操作数     
  3. (3).使用命令:ulimit -a   查看每个用户允许打开的最大文件数     
  4.   默认是1024.     
  5. (4).然后执行:ulimit -n 65536 将允许的最大文件数调整为65536    

 

转自: http://blog.csdn.net/ygd266/article/details/8255746