Java服务器的单线程或多线程池? [英] Single or multiple thread pools for Java server?

查看:199
本文介绍了Java服务器的单线程或多线程池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个相当复杂的Java服务器应用程序,除了通常的请求-响应处理之外,它还具有重要的后台处理部分.一些后台处理是使用Quartz框架以类似cron的方式完成的.其他任务的需求更大-如果有新客户端连接,它将创建额外的作业以不时地进行更新. cron任务也可以更改-有些监视外部应用程序,有些计算统计信息等等.

I am writing a fairly complex Java server application which has a significant background processing portion in addition to the usual request-response processing. Some of the background processing is done in a cron-like fashion using Quartz framework. Other tasks are more of on demand - if a new client connects it creates additional job for updating it once in a while. The cron tasks can also be varied - some do monitoring of external applications, some calculate statistics and so on.

我正在使用许多线程池来运行所有这些作业,以为类似的作业将共享一个线程池,但不同的作业将不会共享一个线程池.例如,监视作业将永远不会在统计池上运行,而统计作业将永远不会在监视池上运行.

I am using a number of thread pools to run all those jobs with the idea that similar jobs will share a thread pool but different jobs will not share one. For example monitor jobs will never run on statistics pool, and statistics jobs will never run on monitor pool.

另一方面,我知道有些人只希望拥有一个线程池并在其上运行所有线程而没有任何分隔.

On the other hand I know some people would prefer just to have a single thread pool and run everything on it without any separation.

我想知道在这种情况下什么是最佳实践.

I wonder what is considered the best practice in such a scenario.

分离线程池的利弊是什么?

What are the pros an cons of separating thread pools?

这有关系吗?

推荐答案

答案取决于您是否需要在不同类型的活动之间隔离应用程序资源.

The answer depends on whether you need to segregate application resources between different types of activity or not.

例如,我当前正在编写一个服务器应用程序,其中包括一些高吞吐量的作者和可能的许多读者.读者会偶尔访问该应用,但可能会请求大量数据(即长时间运行的请求).我需要确保作家永远不会饿死,因此在我的设计中将使用两个线程池进行读取/写入.如果读取器线程池暂时耗尽,则写入器将不受影响;仅读取请求将被延迟.

For example, I am currently writing a server application consisting of a few high-throughput writers and potentially many readers. Readers will access the app sporadically but could potentially request a lot of data (i.e. long running requests). I need to ensure that writers are never starved so am going to use two thread pools in my design for reading / writing. If the reader thread pool is temporarily exhausted the writers will be unaffected; only read requests will be delayed.

另一种选择是将PriorityQueueThreadPoolExecutor结合使用,并为写请求分配更高的优先级.

An alternative would have been to use a PriorityQueue in conjunction with a ThreadPoolExecutor and assign a higher priority to write requests.

总而言之,我的建议是:从一个线程池开始,只有在有具体理由的情况下,才使您的设计更复杂.

So in conclusion - My advice would be: Start off with one thread pool and only make your design more complex if there's a concrete reason for doing so.

这篇关于Java服务器的单线程或多线程池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆