执行程序池限制一次的线程数

时间:2021-02-18 21:06:20

I have a situation in which I have to run some 10,000 threads. Obviously, one machine cannot run these many threads in parallel. Is there any way by which we can ask Thread pool to run some specific number of threads in the beginning and as soon as one thread finishes, the threads which are left can start their processing ?

我有一种情况,我必须运行大约10,000个线程。显然,一台机器不能并行运行这么多线程。有什么方法可以让我们可以让线程池在开始时运行一些特定数量的线程,一旦一个线程完成,剩下的线程就可以开始处理了吗?

4 个解决方案

#1


3  

Executors.newFixedThreadPool(nThreads) is what most likely you are looking for. There will only be as many threads running at one time as the number of threads specified. And yes one machine cannot run 10,000 threads at once in parallel, but it will be able to run them concurrently. Depending on how resource intensive each thread is, it may be more efficient in your case to use Executors.newCachedThreadPool() wherein as many threads are created as needed, and threads that have finished are reused.

Executors.newFixedThreadPool(nThreads)是你最想要的东西。一次运行的线程数与指定的线程数一样多。是的,一台机器不能同时并行运行10,000个线程,但它可以同时运行它们。根据每个线程的资源密集程度,在您的情况下使用Executors.newCachedThreadPool()可能更有效,其中根据需要创建多个线程,并重用已完成的线程。

#2


0  

For this use case. You can have a ThreadPollExecuter with Blocking Queue. http://howtodoinjava.com/core-java/multi-threading/how-to-use-blockingqueue-and-threadpoolexecutor-in-java/ this tutorial explains that very well.

对于这个用例。你可以拥有一个带阻塞队列的ThreadPollExecuter。 http://howtodoinjava.com/core-java/multi-threading/how-to-use-blockingqueue-and-threadpoolexecutor-in-java/本教程非常好地解释了这一点。

#3


0  

It sounds like you want to run 10,000 tasks on a group of threads. A relatively simple approach is to create a List and then add all the tasks to the list, wrapping them in Runnable. Then, create a class that takes the list in the constructor and pops a Runnable of the list and then runs it. This activity must be synchronized in some manner. The class exits when the list is empty. Start some number of threads using this class. They'll burn down the list and then stop. Your main thread can monitor the length of the list.

听起来你想在一组线程上运行10,000个任务。一种相对简单的方法是创建一个List,然后将所有任务添加到列表中,将它们包装在Runnable中。然后,创建一个类,该类获取构造函数中的列表并弹出列表的Runnable然后运行它。必须以某种方式同步此活动。列表为空时,类退出。使用此类启动一些线程。他们会烧掉名单,然后停下来。您的主线程可以监视列表的长度。

#4


0  

Using Executors.newFixedThreadPool(10000) with invokeAll will throw an OutOfMemory exception with that many threads. You still could use it by submitting tasks to it instead of invoking all tasks at same time, that's I would say safer than just invokeAll.

将Executors.newFixedThreadPool(10000)与invokeAll一起使用将抛出具有该多个线程的OutOfMemory异常。你仍然可以通过向它提交任务而不是同时调用所有任务来使用它,我会说比invokeAll更安全。

#1


3  

Executors.newFixedThreadPool(nThreads) is what most likely you are looking for. There will only be as many threads running at one time as the number of threads specified. And yes one machine cannot run 10,000 threads at once in parallel, but it will be able to run them concurrently. Depending on how resource intensive each thread is, it may be more efficient in your case to use Executors.newCachedThreadPool() wherein as many threads are created as needed, and threads that have finished are reused.

Executors.newFixedThreadPool(nThreads)是你最想要的东西。一次运行的线程数与指定的线程数一样多。是的,一台机器不能同时并行运行10,000个线程,但它可以同时运行它们。根据每个线程的资源密集程度,在您的情况下使用Executors.newCachedThreadPool()可能更有效,其中根据需要创建多个线程,并重用已完成的线程。

#2


0  

For this use case. You can have a ThreadPollExecuter with Blocking Queue. http://howtodoinjava.com/core-java/multi-threading/how-to-use-blockingqueue-and-threadpoolexecutor-in-java/ this tutorial explains that very well.

对于这个用例。你可以拥有一个带阻塞队列的ThreadPollExecuter。 http://howtodoinjava.com/core-java/multi-threading/how-to-use-blockingqueue-and-threadpoolexecutor-in-java/本教程非常好地解释了这一点。

#3


0  

It sounds like you want to run 10,000 tasks on a group of threads. A relatively simple approach is to create a List and then add all the tasks to the list, wrapping them in Runnable. Then, create a class that takes the list in the constructor and pops a Runnable of the list and then runs it. This activity must be synchronized in some manner. The class exits when the list is empty. Start some number of threads using this class. They'll burn down the list and then stop. Your main thread can monitor the length of the list.

听起来你想在一组线程上运行10,000个任务。一种相对简单的方法是创建一个List,然后将所有任务添加到列表中,将它们包装在Runnable中。然后,创建一个类,该类获取构造函数中的列表并弹出列表的Runnable然后运行它。必须以某种方式同步此活动。列表为空时,类退出。使用此类启动一些线程。他们会烧掉名单,然后停下来。您的主线程可以监视列表的长度。

#4


0  

Using Executors.newFixedThreadPool(10000) with invokeAll will throw an OutOfMemory exception with that many threads. You still could use it by submitting tasks to it instead of invoking all tasks at same time, that's I would say safer than just invokeAll.

将Executors.newFixedThreadPool(10000)与invokeAll一起使用将抛出具有该多个线程的OutOfMemory异常。你仍然可以通过向它提交任务而不是同时调用所有任务来使用它,我会说比invokeAll更安全。