linux系统线程池

2023-05-16

简述

一个进程中的线程就好比是一家公司里的员工,员工的数目应该根据公司的业务多少来定,太少了忙不过来,但是太多了也浪费资源。最理想的情况是让进程有一些初始数目的线程(线程池),当没有任务时这些线程自动进入睡眠,有了任务它们会立即执行任务,不断循环。进程还应根据自身任务的繁重与否来增删线程的数目,当所有的任务都完成之后,所有的线程还能妥当地收官。

如下图所示是一个处于初始状态的线程池。

1234

​ 图1 线程池示意图

有以下几点需要注意:

(1)任务队列中刚开始没有任何任务,是一个具有头结点的空链队列

(2)使用互斥锁来保护这个队列

(3)使用条件变量来代表任务队列中的任务个数的变化,将来如果主线程向队列中投放任务,那么可以通过条件变量来唤醒那么睡着了的线程

(4)通过一个公共开关----shutdown,来控制线程退出,进而销毁整个线程池

接口设计

线程池相关结构体如下表所示。

原型struct task
功能描述任务节点,包含需要执行的函数及其参数,通过链表连成一个任务队列
成员列表void * (* task)(void *arg);
void *arg;
struct task *next;
备注任务实例最终形成一条单向链表
原型thread_pool
功能描述线程池实例,包含一个线程池的所有信息
成员列表pthread_mutex_t lock; //互斥锁,保护任务队列
pthread_cond_t cond; //条件变量,同步所有线程
bool shutdown; //线程池销毁标记
struct task *task_list; //任务链队列指针
pthread_t *tids; //线程ID存放位置
unsigned int waiting tasks; //任务链队列中等待的任务个数
unsigned int active_threads; //当前活跃线程个数
备注活跃线程个数可以修改,但至少有1条活跃线程

下面是线程池的接口说明

(1)线程池初始化:init_pool()

原型bool init_pool(thread_pool *pool, unsigned int threads_number);
功能描述创建一个新的线程池,包含threads_number个活跃线程
参数pool:线程池指针
threads_number:初始活跃线程个数(大于或等于1)
返回值成功返回true,失败返回false
头文件thread_pool.h
备注线程池最少线程个数为1

(2)投放任务:add_task()

原型bool add_task(thread_pool *pool, void * (*do_task)(void *arg), void *arg);
功能描述往线程池投放任务
参数pool:线程池指针
do_task:投放至线程池的执行例程
arg:执行例程do_task的参数,若该执行例程不需要参数可设置为NULL
返回值成功返回true,失败返回false
头文件thread_pool.h
备注任务队列中最大任务个数为MAX_WAITING_TASKS

(3) 增加活跃线程:add_thread()

原型int add_thread(thread_pool *pool, unsigned int additional_threads);
功能描述增加线程池中活跃线程的个数
参数pool:需要增加线程的线程池指针
additional_threads:新增线程个数
返回值>0 : 实际新增线程个数
-1 :失败
头文件thread_pool.h

(4)删除活跃线程:remove_thread()

原型int remove_thread(thread_pool *pool, unsigned int removing threads);
功能描述删除线程池中活跃线程的个数
参数pool:需要删除线程的线程池指针
removing_threads:要删除的线程个数;该参数设置为0时直接返回当前线程池线程总数,对线程池不造成任何其他影响
返回值>0 : 当前线程池剩余线程个数
-1: 失败
头文件thread_pool.h
备注线程池至少会存在1条活跃线程
如果被删除的线程正在执行任务,就等待其完成任务之后删除

(5)销毁线程池:destroy_pool()

原型bool destroy_pool(thread_pool *pool);
功能描述阻塞等待所有任务完成,然后立即销毁整个线程池,释放所有资源和内存
参数pool:将要销毁的线程池
返回值成功返回true,失败返回false
头文件thread_pool.h

源代码实现

头文件thread_pool.h

//
//  
//  Description: 本文件包含了线程池基本结构体定义,以及各个操作函
//               数的声明
//
//

#ifndef _THREAD_POOL_H_
#define _THREAD_POOL_H_

#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

#include <errno.h>
#include <pthread.h>

#define MAX_WAITING_TASKS	1000
#define MAX_ACTIVE_THREADS	20

struct task
{
	void *(*task)(void *arg);
	void *arg;

	struct task *next;
};

typedef struct thread_pool
{
	pthread_mutex_t lock;
	pthread_cond_t  cond;
	struct task *task_list;

	pthread_t *tids;

	unsigned waiting_tasks;
	unsigned active_threads;

	bool shutdown;
}thread_pool;


bool
init_pool(thread_pool *pool,
          unsigned int threads_number);

bool
add_task(thread_pool *pool,
         void *(*task)(void *arg),
         void *arg);

int 
add_thread(thread_pool *pool,
           unsigned int additional_threads_number);

int 
remove_thread(thread_pool *pool,
              unsigned int removing_threads_number);

bool destroy_pool(thread_pool *pool);
void *routine(void *arg);

#endif

接口实现:thread_pool.c

//
//  Description: 本文件包含了线程池操作函数的定义
//

#include "thread_pool.h"

void handler(void *arg)
{

	pthread_mutex_unlock((pthread_mutex_t *)arg);
}

void *routine(void *arg)
{
	thread_pool *pool = (thread_pool *)arg;
	struct task *p;

	while(1)
	{

		pthread_cleanup_push(handler, (void *)&pool->lock);
		pthread_mutex_lock(&pool->lock);


		while(pool->waiting_tasks == 0 && !pool->shutdown)
		{
			pthread_cond_wait(&pool->cond, &pool->lock);
		}


		if(pool->waiting_tasks == 0 && pool->shutdown == true)
		{
			pthread_mutex_unlock(&pool->lock);
			pthread_exit(NULL);
		}


		p = pool->task_list->next;
		pool->task_list->next = p->next;
		pool->waiting_tasks--;


		pthread_mutex_unlock(&pool->lock);
		pthread_cleanup_pop(0);


		pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
		(p->task)(p->arg);
		pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);

		free(p);
	}

	pthread_exit(NULL);
}

bool init_pool(thread_pool *pool, unsigned int threads_number)
{
	pthread_mutex_init(&pool->lock, NULL);
	pthread_cond_init(&pool->cond, NULL);

	pool->shutdown = false;
	pool->task_list = malloc(sizeof(struct task));
	pool->tids = malloc(sizeof(pthread_t) * MAX_ACTIVE_THREADS);

	if(pool->task_list == NULL || pool->tids == NULL)
	{
		perror("allocate memory error");
		return false;
	}

	pool->task_list->next = NULL;

	pool->waiting_tasks = 0;
	pool->active_threads = threads_number;

	int i;
	for(i=0; i<pool->active_threads; i++)
	{
		if(pthread_create(&((pool->tids)[i]), NULL,
					routine, (void *)pool) != 0)
		{
			perror("create threads error");
			return false;
		}
	}

	return true;
}

bool add_task(thread_pool *pool,
			void *(*task)(void *arg), void *arg)
{
	struct task *new_task = malloc(sizeof(struct task));
	if(new_task == NULL)
	{
		perror("allocate memory error");
		return false;
	}
	new_task->task = task;
	new_task->arg = arg;
	new_task->next = NULL;


	pthread_mutex_lock(&pool->lock);
	if(pool->waiting_tasks >= MAX_WAITING_TASKS)
	{
		pthread_mutex_unlock(&pool->lock);

		fprintf(stderr, "too many tasks.\n");
		free(new_task);

		return false;
	}
	
	struct task *tmp = pool->task_list;
	while(tmp->next != NULL)
		tmp = tmp->next;

	tmp->next = new_task;
	pool->waiting_tasks++;


	pthread_mutex_unlock(&pool->lock);
	pthread_cond_signal(&pool->cond);

	return true;
}

int add_thread(thread_pool *pool, unsigned additional_threads)
{
	if(additional_threads == 0)
		return 0;

	unsigned total_threads =
		     pool->active_threads + additional_threads;

	int i, actual_increment = 0;
	for(i = pool->active_threads;
	    i < total_threads && i < MAX_ACTIVE_THREADS;
	    i++)
	{
		if(pthread_create(&((pool->tids)[i]),
				NULL, routine, (void *)pool) != 0)
		{
			perror("add threads error");

			if(actual_increment == 0)
				return -1;

			break;
		}
		actual_increment++; 
	}

	pool->active_threads += actual_increment;
	return actual_increment;
}

int remove_thread(thread_pool *pool, unsigned int removing_threads)
{
	if(removing_threads == 0)
		return pool->active_threads;

	int remain_threads = pool->active_threads - removing_threads;
	remain_threads = remain_threads>0 ? remain_threads:1;

	int i;
	for(i=pool->active_threads-1; i>remain_threads-1; i--)
	{
		errno = pthread_cancel(pool->tids[i]);
		if(errno != 0)
			break;
	}

	if(i == pool->active_threads-1)
		return -1;
	else
	{
		pool->active_threads = i+1;
		return i+1;
	}
}

bool destroy_pool(thread_pool *pool)
{

	pool->shutdown = true;
	pthread_cond_broadcast(&pool->cond);

	int i;
	for(i=0; i<pool->active_threads; i++)
	{
		errno = pthread_join(pool->tids[i], NULL);
		if(errno != 0)
		{
			printf("join tids[%d] error: %s\n",
					i, strerror(errno));
		}
		else
			printf("[%u] is joined\n", (unsigned)pool->tids[i]);
		
	}

	free(pool->task_list);
	free(pool->tids);
	free(pool);

	return true;
}

测试程序

main.c

//

//  Description: 一个使用了线程池的示例

//

#include "thread_pool.h"

void *mytask(void *arg)
{
	int n = (int)arg;

	printf("[%u][%s] ==> job will be done in %d sec...\n",
		(unsigned)pthread_self(), __FUNCTION__, n);

	sleep(n);

	printf("[%u][%s] ==> job done!\n",
		(unsigned)pthread_self(), __FUNCTION__);

	return NULL;
}

void *count_time(void *arg)
{
	int i = 0;
	while(1)
	{
		sleep(1);
		printf("sec: %d\n", ++i);
	}
}

int main(void)
{
	pthread_t a;
	pthread_create(&a, NULL, count_time, NULL);

	// 1, initialize the pool
	thread_pool *pool = malloc(sizeof(thread_pool));
	init_pool(pool, 2);

	// 2, throw tasks
	printf("throwing 3 tasks...\n");
	add_task(pool, mytask, (void *)(rand()%10));
	add_task(pool, mytask, (void *)(rand()%10));
	add_task(pool, mytask, (void *)(rand()%10));

	// 3, check active threads number
	printf("current thread number: %d\n",
			remove_thread(pool, 0));
	sleep(9);

	// 4, throw tasks
	printf("throwing another 2 tasks...\n");
	add_task(pool, mytask, (void *)(rand()%10));
	add_task(pool, mytask, (void *)(rand()%10));

	// 5, add threads
	add_thread(pool, 2);

	sleep(5);

	// 6, remove threads
	printf("remove 3 threads from the pool, "
	       "current thread number: %d\n",
			remove_thread(pool, 3));

	// 7, destroy the pool
	destroy_pool(pool);
	return 0;
}

运行结果

zzc@zzc-virtual-machine:~/share/example/thread$ ./test 
throwing 3 tasks...
current thread number: 2
[3981727488][mytask] ==> job will be done in 3 sec...
[3990120192][mytask] ==> job will be done in 6 sec...
sec: 1
sec: 2
[3981727488][mytask] ==> job done!
[3981727488][mytask] ==> job will be done in 7 sec...
sec: 3
sec: 4
sec: 5
[3990120192][mytask] ==> job done!
sec: 6
sec: 7
sec: 8
throwing another 2 tasks...
[3990120192][mytask] ==> job will be done in 5 sec...
[3892311808][mytask] ==> job will be done in 3 sec...
sec: 9
[3981727488][mytask] ==> job done!
sec: 10
sec: 11
[3892311808][mytask] ==> job done!
sec: 12
sec: 13
remove 3 threads from the pool, current thread number: 1
[3990120192][mytask] ==> job done!
[3990120192] is joined

延伸

实际使用的时候,只需将上述代码中的mytask函数改成我们需要实现的功能函数即可。

总结

本文简单介绍了线程池的概念和特点,对线程池的结构体和相关操作接口进行了设计,并提供了接口的具体实现,最后通过示例程序演示了线程池的运行过程。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

linux系统线程池 的相关文章

  • 用C语言实现一个简单的HTTP客户端(HTTP Client)

    用C语言实现一个简单的HTTP Client xff08 HTTP客户端 xff09 作者 xff1a gobitan xff08 雨水 xff09 日期 xff1a 2007 04 03 转载请注明出处 http blog csdn ne
  • C语言常见的自定义数据类型(1)—— 结构体

    目录 1 结构体 1 1 结构体的定义 1 2 结构体的自引用 1 3 结构体类型的重命名 1 4 结构体的嵌套 2 结构体大小的计算 2 1 结构体内存对齐 2 2 嵌套结构体大小的计算 2 3 offsetof函数 2 4 修改默认对齐
  • 一篇解决!小白迷惑:Go mod本地包导入

    最近在学习go xff0c 在导入本都包遇到一个问题 xff0c 根据网上许多教程来都走不通 xff0c 最后在官网得到了最正确的答案 官网教程 xff1a Call your code from another module The Go
  • Linux nf_conntrack连接跟踪的实现

    连接跟踪 xff0c 顾名思义 xff0c 就是识别一个连接上双方向的数据包 xff0c 同时记录状态 下面看一下它的数据结构 xff1a struct nf conn Usage count in here is 1 for hash t
  • 组播MAC地址和各类IP地址

    MAC地址是以太网二层使用的一个48bit xff08 6字节十六进制数 xff09 的地址 xff0c 用来标识设备位置 MAC地址分成两部分 xff0c 前24位是组织唯一标识符 xff08 OUI Organizationally u
  • Linux内核中的软中断、tasklet和工作队列详解

    TOC 本文基于Linux2 6 32内核版本 引言 软中断 tasklet和工作队列并不是Linux内核中一直存在的机制 xff0c 而是由更早版本的内核中的 下半部 xff08 bottom half xff09 演变而来 下半部的机制
  • Linux内核中的各种锁

    Linux内核中的各种锁 在现代操作系统里 xff0c 同一时间可能有多个内核执行流在执行 xff0c 因此内核其实象多进程多线程编程一样也需要一些同步机制来同步各执行单元对共享数据的访问 尤其是在多处理器系统上 xff0c 更需要一些同步
  • Linux进程间通信方式

    进程与进程通信的概念进程通信的应用场景进程通信的几种方式 管道 管道简介管道原理 管道如何通信管道如何创建管道读写实现 管道api与用法 普通管道流管道命名管道 实现原理api与应用 匿名管道和有名管道总结 信号 信号来源信号生命周期和处理
  • C语言字节对齐问题详解

    引言 考虑下面的结构体定义 xff1a span class hljs keyword typedef span span class hljs keyword struct span span class hljs built in ch
  • git使用

    git使用 git clone时报如下错误 原因解决方法 TortoiseGit clone时报错 问题原因解决方法 git log使用git回归代码 git使用 本文记录的是工作中git是使用问题 xff0c 无脑模式 xff0c 遇到什
  • 内存对齐算法

    字节对齐是在分配内存时需要考虑的问题 xff0c 两个小算法 xff1a 1 最容易想到的算法 unsigned int calc align unsigned int n unsigned align if n align align 6
  • vscode快捷键整理

    1 注释 xff08 1 xff09 方式 注释 取消注释 xff1a Ctrl 43 xff08 2 xff09 方式 注释 xff1a Ctrl 43 Shift 43 取消注释 xff1a Ctrl 43 Shift 43 2 代码排
  • Qt之实现移动的方块(蚂蚁线)

    一 简介 移动的小方块或者说是类似移动的蚂蚁线 xff0c 从一篇文章看到的 xff0c 挺有趣的就自己做了一个 xff0c 可以自由添加方块的个数 xff0c 起始位置 xff0c 方块的宽度 xff0c 方块移动速度等待参数 xff0c
  • Docker 突然挂掉 failed to create shim task: OCI runtime create failed: container_linux.go:345: ...

    目录 问题描述 xff1a 参考 解决方案 最佳方案 xff1a 问题描述 xff1a docker Error response from daemon failed to create shim task OCI runtime cre
  • Qt之事件过滤器(eventFilter)详解

    1 2 1 Qt中事件是如何进行传递 1 2 2 Qt中的事件过滤器 xff08 eventFilter xff09 1 2 3 如何自己模拟发送事件消息 一 Qt中事件过滤器详解 我们先看下另外两个相关的方法 xff0c 一个是给对象安装
  • Qt实现微信截图功能(一)

    简述 Qt 之 简单截图功能 xff08 一 xff09 实现鼠标选中区域截图Qt 之 简单截图功能 xff08 二 xff09 实现可移动选中区域Qt 之 简单截图功能 xff08 三 xff09 实现可拖拽选中区域 在之前的文章中有带大
  • Qt之QMenu菜单去除投影效果(阴影)

    一 简述 我们使用Qt中的菜单 xff0c 正常情况下样式是跟随当前系统菜单的样式 xff0c 我们可以使用样式表进行修饰 xff0c 改变原有风格 xff0c 但是window系统上菜单边框四周会附带阴影的效果 xff0c 样式是无法取消
  • Qt 之 设置窗口边框的圆角

    Qt技术学习班开始了 xff0c 更多精彩 好玩的内容等着你 xff0c 赶紧报名吧 群号 xff1a 655815739 Qt在设置窗口边框圆角时有两种方式 xff0c 一种是设置样式 xff0c 另一种是在paintEvent事件中绘制
  • Qt 之 HTTP 请求下载(支持断点续传)

    简述 最近在研究了一下用Qt 的方法来实现http下载 xff0c Qt 中的Http请求主要用到了QNetworkAccessManager QNetworkReply QNetworkRequest 这三块 本篇文章主要叙述如何用Qt
  • Qt之实现录音播放及raw(pcm)转wav格式

    简述 在上一篇 Qt 之 WAV文件解析 中详细地分析了wav格式文件的文件头信息 通过QAudioInput实现录音功能 xff0c 但是录音生成的文件并不能用播放器打开 xff0c 就算更改后缀名也无法识别 xff08 有时候下载的一些

随机推荐

  • C++中 Unicode 与 UTF-8 编码互转

    1 简述 最近在发送网络请求时遇到了中文字符乱码的问题 xff0c 在代码中调试字符正常 xff0c 用抓包工具抓的包中文字符显示正常 xff0c 就是发送到服务器就显示乱码了 xff0c 那就要将客户端和服务器设置统一的编码 xff08
  • Qt 之 自定义按钮 在鼠标 悬浮、按下、松开后的效果

    Qt技术学习班开始了 xff0c 更多精彩 好玩的内容等着你 xff0c 赶紧报名吧 群号 xff1a 655815739 一 简述 在上一篇 Qt 之 去除窗口部件被选中后的焦点虚线框 中 xff0c 我们为了去除焦点虚线框 xff0c
  • Qt 之 自定义窗口标题栏

    Qt训练营开始了 xff0c 更多精彩 好玩的内容等着你 xff0c 赶紧报名吧 群号 xff1a 861353824 一 简述 今天晚上就如何用Qt自定义窗口标题栏 xff0c 写了一个小例子 xff0c 比较基础 xff0c 实用 在此
  • Qt 之 模仿 QQ登陆界面——旋转窗口篇

    一 简述 今天是新的一年第一篇博客 xff0c 有大半个月没有更新博客了 我想是时候 xff0c 打开电脑 拿起键盘 开始在我的代码之路上披荆斩棘 xff0c 斩杀恶龙 今天就继续来分享QQ登录界面的那些事 QQ登录界面的标题栏有一个小三角
  • Ubuntu配置无线路由器笔记记录

    参考文章 xff1a linux 开启制作无线路由器 ubuntu 1404 linux zhu的博客 CSDN博客 hostapd实现WIFI 热点 xff08 AP xff09 自由枫 的博客 CSDN博客 hostapd 终端get一
  • C++STL的使用心得汇总(vector,string,map,list)

    文章目录 find 函数vector的findstring的findmap的find count 函数vector的countstring的countmap的count vectorstringmap的各种排序方法转换相关 待完善 find
  • Qt 之 样式表的使用——设置样式的方法

    一 简述 我们通常在使用Qt开发的过程中都会使用样式表来美化我们的界面 xff0c 关于如何使用样式表的资料也很多 xff0c 样式表的使用方法也千变万化 为了搭建一个漂亮的界面那么必须学会如何使用样式表 xff0c Qt帮助文档中提供了非
  • 如何使QGraphicsItem不随QGraphicsView放大缩小而改变大小

    一 简述 在使用QGraphicsView过程中 xff0c 有时候我们需要对view进行缩放 xff0c 但是对于一般正常的加入view中的item都会随着view的大小变化而变化 xff0c 但是如果我们想让某些item不随view的缩
  • 【linux系统如何查看内核版本、操作系统版本等信息】

    有时候需要查看linux系统的内核版本 xff0c 可以有多种方法 xff0c 方法如下 xff1a xff08 下面以优麒麟系统为例 xff09 方法1 xff1a 打开mate终端 xff0c 在命令行输入以下命令 xff1a unam
  • 【linux系统如何安装arm交叉编译工具链】

    文章目录 前言一 arm交叉编译器介绍命名规则具体编译器 二 Arm GNU Toolchain安装总结 前言 本文简要介绍arm交叉编译器及工具链的安装方法 一 arm交叉编译器介绍 命名规则 交叉编译工具链的命名规则为 xff1a ar
  • 比较冒泡排序、选择排序和快速排序的时间(C语言实现)

    文章目录 前言代码设计代码实现运行结果结果分析稳定性测试 总结 前言 本文主要比较冒泡排序 快速排序 选择排序的时间 冒泡排序和快速排序的思想可以参考我转载的以下博文 xff1a https blog csdn net gogo0707 a
  • freertos应用程序常见错误排查

    freertos系统应用程序常见问题 对一些比较常见的问题 xff0c 下面简要的以 FAQ 问答 的形式给出可能的原因和解决方法 问题现象 xff1a 在一个 Demo 应用程序中增加了一个简单的任务 xff0c 导致应用程序崩溃 任务创
  • keil5编译工程常见问题汇总

    简介 我们在编译keil工程的时候总是遇到很多问题 xff0c 我把一些常见的问题和解决方案汇总下来 xff0c 仅供大家参考 问题汇总 问题1 问题描述 选择arm v6版本编译器 xff0c 编译keil5工程 xff0c 报错 xff
  • mdk arm debug配置

    简述 本文简要讲述启动调试之前如何配置debug 点击魔术棒 xff0c 进入debug选项界面 xff0c 如下图 xff1a 我们可以选择软件仿真 xff0c 也可以选择硬件仿真 xff08 软件仿真不需要接开发板和仿真器 xff09
  • stm32高级定时器实现pwm互补输出

    简介 stm32设备一般都有很多类型的定时器 xff0c 常见的有systick timer 基本定时器 通用定时器 高级定时器 看门狗定时器 RTC等等 xff0c 本文简单介绍高级定时器是如何实现pwm互补输出 详细 我这里使用的dev
  • shell脚本基础知识(入门)

    简介 本文会全面介绍shell脚本的基础知识 脚本格式 要把shell命令放到一个 脚本 当中 xff0c 有一个要求 xff1a 脚本的第一行必须写成类似这样的格式 xff1a bin bash bash是一个shell解释器 xff0c
  • 记ADB shell for循环踩坑

    abd 里面的shell的电脑Linux的shell有点不太一样 以下这些案例均不能执行 xff1a for i 61 1 i lt 61 100 i 43 43 do echo i done for i in 1 100 do echo
  • linux线程调度策略简述

    简述 linux系统调度执行的最小单位是线程 xff0c 线程的调度策略有以下三种 xff1a xff08 1 xff09 SCHED FIFO 其静态优先级必须设置为1 99 xff0c 这将意味着一旦线程处于就绪态 xff0c 他就能立
  • stm32串口发送接口

    简介 本文记录一下stm32标准库实现串口发送功能和接收功能的接口函数 轮询方式发送串口数据 1 标准库实现 span class token comment 61 61 61 61 61 61 61 61 61 61 61 61 61 6
  • linux系统线程池

    简述 一个进程中的线程就好比是一家公司里的员工 xff0c 员工的数目应该根据公司的业务多少来定 xff0c 太少了忙不过来 xff0c 但是太多了也浪费资源 最理想的情况是让进程有一些初始数目的线程 xff08 线程池 xff09 xff