当前线程由apache动态管理。
来自apache源代码中的worker.c:线程根据需要启动。以下是启动工作线程的主要流程。
static int worker_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
startup_children(remaining_children_to_start); //server/mpm/worker/worker.c
if (make_child(ap_server_conf, i) < 0) { //server/mpm/worker/worker.c
child_main(slot); //server/mpm/worker/worker.c
rv = apr_thread_create(&start_thread_id, thread_attr, start_threads
rv = apr_thread_create(&threads[i], thread_attr, worker_thread, my_info, pchild);
并且当空闲线程> max_spare_threads 时,apache 将尝试通过杀死子进程/线程来减少空闲线程以减少空闲线程数。
if (idle_thread_count > max_spare_threads) {
/* Kill off one child */
ap_worker_pod_signal(pod, TRUE);
retained->idle_spawn_rate = 1;
}
apache在内部管理这些子进程,并使用信号与子进程通信以根据需要调整线程。所有线程都在ap_scoreboard_image->servers[i][j];
apache 将监视线程数,并使用套接字发送让 infoCollector 知道。大致通过以下代码(删除了无关的行)
./httpd/modules/cluster/mod_heartbeat.c
for (i = 0; i < ctx->server_limit; i++) {
....
for (j = 0; j < ctx->thread_limit; j++) {
ws = &ap_scoreboard_image->servers[i][j];
if (res == SERVER_READY && ps->generation == mpm_generation) {
ready++;
}
}
}
len = apr_snprintf(buf, sizeof(buf), "v=%u&ready=%u&busy=%u", MSG_VERSION, ready, busy);
...
rv = apr_socket_sendto(sock, ctx->mcast_addr, 0, buf, &len);
在 Tomcat 中:
这些线程信息通过以下方式接收:./tomcat/java/org/apache/catalina/ha/backend/CollectedInfo.java 并通过以下方式显示:/Users/twer/lab/tomcat/java/org/apache/catalina/经理/StatusTransformer.java
writer.write(" currentThreadCount=\"" + mBeanServer.getAttribute(tpName, "currentThreadCount") + "\"");
这里显示的大部分代码都是主要流程,无关的行被删除。下载源代码以了解更多信息。下载apache src:http ://www.apache.org/dist/httpd/?C=S;O=A
下载tomcat src:http: //tomcat.apache.org/download-70.cgi