-2

Why does creating a new bootstrap have to look so awkward? There should be an easier way to do this than the usual

serverBootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
    Executors.newCachedThreadPool(),
    Executors.newCachedThreadPool()));

The way it looks is absolutely horrid. Like, why do I have to call newCachedThreadPool() twice?.

Siguza
  • 21,155
  • 6
  • 52
  • 89

1 Answers1

0

In recent versions of Netty, you can use the default NioServerSocketChannelFactory constructor, which uses two cached thread pools like you are currently doing. The boostrap instantiation would then look like this, which is much less verbose:

ServerBootstrap sb = new ServerBootstrap(new NioServerSocketChannelFactory());

Please research what these thread pools are used for, however. Under certain conditions (like slow loris attacks), this setup will not work very well and instead will cause the Java process to run out of heap space. You may want to look into the MemoryAwareThreadPoolExecutor class provided by Netty for this reason.

Cached thread pools are inherently evil, because they have a point where they reach "critical mass." This is a situation where performance degrades due to the overhead of context switching when there is a very high thread count. As performance degrades due to thread count, the thread pool workers finish their tasks more slowly. As these workers are busy for longer periods of time, the cached thread pool must continue to create new threads to handle the continuing work load. As more threads are created, performance continues to degrade event more. The problem contributes to itself, and your server essentially explodes. Have fun!

Blake Beaupain
  • 638
  • 1
  • 6
  • 16