Grizzy之Best Practices

When developing a network application, we usually wonder how we can optimize it. How should the worker thread pool be sized? Which I/O strategy to employ?

There is no general answer for that question, but well try to provide some tips.

  • IOStrategy

    In the IOStrategy section, we introduced different Grizzly IOStrategies.

    By default, Grizzly Transports use the worker-thread IOStrategy, which is reliable for any possible usecase. However, if the application processing logic doesnt involve any blocking I/O operations, the same-thread IOStrategy can be used. For these cases, the same-thread strategy will be more performant as there are no thread context switches.

    For example, if we implement general HTTP Servlet container, we cant be sure about nature of specific Servlets developers may have. In this case its safter to use the worker-thread IOStrategy. However, if application uses the Grizzlys HttpServer and HttpHandler, which leverages NIO streams, then the same-thread strategy could be used to optimize processing time and resource consumption;

  • Selector runners count

    The Grizzly runtime will automatically set the SelectorRunner count value equal to [Runtime.getRuntime().availableProcessors()](http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html#availableProcessors()). Depending on the usecase, developers may change this value to better suit their needs.

    Scott Oaks, from the Glassfish performance team, suggests that there should be one SelectorRunner for every 1-4 cores on your machine; no more than that;

  • Worker thread pool

    In the Configuration threadpool-config section, the different thread pool implementations, and their pros and cons, were discussed.

    All IOStrategies, except the same-thread IOStrategy, use worker threads to process IOEvents which occur on Connections. A common question is how many worker threads will be needed by an application?

    In his blog, Scott suggests How many is just enough? It depends, of course in a case where HTTP requests dont use any external resource and are hence CPU bound, you want only as many HTTP request processing threads as you have CPUs on the machine. But if the HTTP request makes a database call (even indirectly, like by using a JPA entity), the request will block while waiting for the database, and you could profitably run another thread. So this takes some trial and error, but start with the same number of threads as you have CPU and increase them until you no longer see an improvement in throughput.

    Translating this to the general, non HTTP usecase: If IOEvent processing includes blocking I/O operation(s), which will make thread block doing nothing for some time (i.e, waiting for a result from a peer), its best to have more worker threads to not starve other request processing. For simpler application processes, the fewer threads, the better.

    https://grizzly.java.net/bestpractices.html

转载于:https://my.oschina.net/chenleijava/blog/141338

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值