java服务器多线程,Java服务器的单线程或多线程池?

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?

Does it even matter?

解决方案

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值