1

In netty I create a channel factory as follows.

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

When a new request comes, how does the boss thread allocate a free worker thread to the new request from the workerthread pool? In which netty class can I find this logic?

Usha
  • 374
  • 1
  • 5
  • 14

2 Answers2

1

You can find the logic in NioServerBoss.class. Following is part of the netty source code.

   for (;;) {
      SocketChannel acceptedSocket = channel.socket.accept();
      if (acceptedSocket == null) {
          break;
      }
      registerAcceptedChannel(channel, acceptedSocket, thread);
   }

Part of registerAcceptedChannel:

NioWorker worker = parent.workerPool.nextWorker();
worker.register(new NioAcceptedSocketChannel(
        parent.getFactory(), pipeline, parent, sink
        , acceptedSocket,
        worker, currentThread), null);

The worker thread will check its taskQueue and run this new task.

Rohit Banga
  • 18,458
  • 31
  • 113
  • 191
zhquake
  • 296
  • 2
  • 9
0

see here

Boss threads Each bound ServerSocketChannel has its own boss thread.

A boss thread accepts incoming connections until the port is unbound. Once a connection is accepted successfully, the boss thread passes the accepted Channel to one of the worker threads that the NioServerSocketChannelFactory manages.

from How Netty uses thread pools?

and now some thing will be improve

netty-issues-240

Community
  • 1
  • 1
yaphet
  • 310
  • 2
  • 8