문제

In my netty server, I create threadpools as follows.

    ChannelFactory factory =
        new NioServerSocketChannelFactory(
            Executors.newCachedThreadPool(threadFactory),
            Executors.newCachedThreadPool(threadFactory);

Sometimes, I noticed that after a certain number of connections are being worked on by the server, the subsequent connections wait for one of the priors threads to finish.

From the documentation of newCachedThreadPool, I was under the assumption that the thread pool creates new threads as needed. Could someone help me understand why some of my connections being blocked till the prior connections finish? Would netty not create a new thread for new connection, as all the existing ones are busy?

How do I fix this?

Any help is appreciated!

도움이 되었습니까?

해결책

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue.

At any point, at most nThreads threads will be active processing tasks.

If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.

The threads in the pool will exist until it is explicitly shutdown.

from Oracle Java Doc for newCachedThreadPool!

so the thread number is fixed by Executors.newCachedThreadPool

in netty default number is processer_number *2

:)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top