简体   繁体   English

如何限制同时运行的线程数

[英]how to bound the number of threads running simultaneously

Let's say i've got a list which holds 100 threads inside it. 假设我有一个列表,其中包含100个线程。

i want to allow only 5 threads run simultaneously until all my threads in list have done their job. 我想只允许5个线程同时运行,直到列表中的所有线程都完成了工作。

i also can't use the java threadpool for this task. 我也不能使用Java线程池执行此任务。

could you give me a clue how to bound the number of my running threads? 你能给我一个线索来如何限制我正在运行的线程数吗?

I think what you need is semaphore . 我认为您需要的是信号量 Check the doc. 检查文档。 Briefly speaking, semaphore is a locking method. 简而言之,信号量是一种锁定方法。 You ask the resource from the semaphore singleton class, which is process scope unique, control the running number of function process. 您可以从信号量单例类(该过程范围是唯一的)中请求资源,以控制函数进程的运行数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM