对线程池的理解

线程池和装修公司

以运营一家装修公司做个比喻。公司在办公地点等待客户来提交装修请求;公司有固定数量的正式工以维持运转;旺季业务较多时,新来的客户请求会被排期,比如接单后告诉用户一个月后才能开始装修;当排期太多时,为避免用户等太久,公司会通过某些渠道(比如人才市场、熟人介绍等)雇佣一些临时工(注意,招聘临时工是在排期排满之后);如果临时工也忙不过来,公司将决定不再接收新的客户,直接拒单。

线程池就是程序中的“装修公司”,代劳各种脏活累活。上面的过程对应到线程池上:

// Java线程池的完整构造函数

public ThreadPoolExecutor(

int corePoolSize, // 正式工数量

int maximumPoolSize, // 工人数量上限,包括正式工和临时工

long keepAliveTime, TimeUnit unit, // 临时工游手好闲的最长时间,超过这个时间将被解雇

BlockingQueue<Runnable> workQueue, // 排期队列

ThreadFactory threadFactory, // 招人渠道

RejectedExecutionHandler handler) // 拒单方式

 

拷贝于:https://www.cnblogs.com/CarpenterLee/p/9558026.html

以运营一家装修公司做个比喻。公司在办公地点等待客户来提交装修请求;公司有固定数量的正式工以维持运转;旺季业务较多时,新来的客户请求会被排期,比如接单后告诉用户一个月后才能开始装修;当排期太多时,为避免用户等太久,公司会通过某些渠道(比如人才市场、熟人介绍等)雇佣一些临时工(注意,招聘临时工是在排期排满之后);如果临时工也忙不过来,公司将决定不再接收新的客户,直接拒单。
[Yǐ yùnyíng yījiā zhuāngxiū gōngsī zuò gè bǐyù. Gōngsī zài bàngōng dìdiǎn děngdài kèhù lái tíjiāo zhuāngxiū qǐngqiú; gōngsī yǒu gùdìng shùliàng de zhèngshì gōng yǐ wéichí yùnzhuǎn; wàngjì yèwù jiào duōshí, xīn lái de kèhù qǐngqiú huì bèi pái qī, bǐrú jiē dān hòu gàosù yònghù yīgè yuè hòu cáinéng kāishǐ zhuāngxiū; dāng pái qī tài duōshí, wèi bìmiǎn yònghù děng tài jiǔ, gōngsī huì tōngguò mǒu xiē qúdào (bǐrú réncái shìchǎng, shúrén jièshào děng) gùyōng yīxiē línshí gōng (zhùyì, zhāopìn línshí gōng shì zài pái qī pái mǎn zhīhòu); rúguǒ línshí gōng yě máng bùguò lái, gōngsī jiāng juédìng bù zài jiēshōu xīn de kèhù, zhíjiē jù dān.]
To operate a decoration company to be a metaphor. The company waiting in office customers to submit requests decoration; the company has a fixed number of regular workers in order to maintain the operation; the peak season when more business, new client requests will be ranked on such orders after a month to tell the user to start renovation; when too much schedule, to avoid user wait too long, the company will be through some channels (such as the job market, acquaintances, etc.) hire temporary workers (Note that after the recruitment of temporary workers filled in the schedule); if temporary workers too busy, the company will decide not to receive new clients, refused direct orders.
 
线程池和装修公司 以运营一家装修公司做个比喻。公司在办公地点等待客户来提交装修请求;公司有固定数量的正式工以维持运转;旺季业务较多时,新来的客户请求会被排期,比如接单后告诉用户一个月后才能开始装修;当排期太多时,为避免用户等太久,公司会通过某些渠道(比如人才市场、熟人介绍等)雇佣一些临时工(注意,招聘临时工是在排期排满之后);如果临时工也忙不过来,公司将决定不再接收新的客户,直接拒单。 线程池就是程序中的“装修公司”,代劳各种脏活累活。上面的过程对应到线程池上:
[Xiànchéng chí hé zhuāngxiū gōngsī yǐ yùnyíng yījiā zhuāngxiū gōngsī zuò gè bǐyù. Gōngsī zài bàngōng dìdiǎn děngdài kèhù lái tíjiāo zhuāngxiū qǐngqiú; gōngsī yǒu gùdìng shùliàng de zhèngshì gōng yǐ wéichí yùnzhuǎn; wàngjì yèwù jiào duōshí, xīn lái de kèhù qǐngqiú huì bèi pái qī, bǐrú jiē dān hòu gàosù yònghù yīgè yuè hòu cáinéng kāishǐ zhuāngxiū; dāng pái qī tài duōshí, wèi bìmiǎn yònghù děng tài jiǔ, gōngsī huì tōngguò mǒu xiē qúdào (bǐrú réncái shìchǎng, shúrén jièshào děng) gùyōng yīxiē línshí gōng (zhùyì, zhāopìn línshí gōng shì zài pái qī pái mǎn zhīhòu); rúguǒ línshí gōng yě máng bùguò lái, gōngsī jiāng juédìng bù zài jiēshōu xīn de kèhù, zhíjiē jù dān. Xiànchéng chí jiùshì chéngxù zhōng de “zhuāngxiū gōngsī”, dàiláo gè zhǒng zàng huó lèi huó. Shàngmiàn de guòchéng duìyìng dào xiànchéng chíshàng:]
Thread pool and decoration companies

To operate a decoration company to be a metaphor. The company waiting in office customers to submit requests decoration; the company has a fixed number of regular workers in order to maintain the operation; the peak season when more business, new client requests will be ranked on such orders after a month to tell the user to start renovation; when too much schedule, to avoid user wait too long, the company will be through some channels (such as the job market, acquaintances, etc.) hire temporary workers (Note that after the recruitment of temporary workers filled in the schedule); if temporary workers too busy, the company will decide not to receive new clients, refused direct orders.

Thread Pool is a program of "decoration company," do it all kinds of dirty work. The above procedure corresponds to the thread pool:
 

转载于:https://www.cnblogs.com/xiaxianxiaoquan/p/11555168.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值