简体   繁体   English

优化我的线程池

[英]Optimizing my thread pool

I have an application where I use Executors for creating a thread pool. 我有一个应用程序,我使用Executors来创建一个线程池。

Now, I know we can create a thread pool with n number of threads in it while creating. 现在,我知道我们可以在创建时创建一个包含n个线程的线程池。

But in my case I don't want the pool size to be fixed. 但就我而言,我不希望修复池大小。 I want the application to create more threads in the pool if the users are more and automatically reduce the pool size if the users are less. 我希望应用程序在池中创建更多线程(如果用户更多),并在用户较少时自动减小池大小。

Please tell me if I'm vague and you need more information on understanding this. 请告诉我,如果我是模糊的,你需要更多的信息来理解这一点。

Cheers!! 干杯!!

You probably want to use Executors.newCachedThreadPool() . 您可能想要使用Executors.newCachedThreadPool() Threads are created as needed, and if they go unused for a while, they will self-terminate. 线程是根据需要创建的,如果它们暂时不使用,它们将自行终止。

In general the ExecutorService has default policies that would provide out-of-the-box values for various tuning parameters for newly created threads. 通常,ExecutorService具有默认策略,这些策略将为新创建的线程提供各种调整参数的开箱即用值。

Based on my understanding of your question, IMO you can control the amount of time idle threads are allowed to live within a thread-pool by using the setKeepAliveTime method. 根据我对您的问题的理解,您可以使用setKeepAliveTime方法控制允许空闲线程在线程池中存在的setKeepAliveTime

You can refer to the documentation here for more details. 您可以参考此处的文档以获取更多详细信息。

Also this link provides good resources to read about thread pools and executors available in Java 链接还提供了很好的资源来读取Java中可用的线程池和执行程序

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

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