我知道網狀使用反應堆模式,以避免爲每個連接創建線程, 這個模式的核心理念是「選擇」,或在Linux中epoll
系統調用。工作線程Netty中
但我也聽說過,如果一個處理器從來沒有關閉的通道,這將佔用一個工作線程並阻止它:並不意味着每個引黃將使用(塊)一個線程,所以每個接受的套接字我們仍然需要創建一個線程?
例如,如果我編寫一個具有10,000個持久連接的服務器,該服務器是否需要10,000個工作線程?
上面這兩件事之間的矛盾讓我困惑,任何人都可以解釋我,如果我理解錯了嗎?謝謝〜
========================================
一個例子(只有一個工作者線程)可以在同一時間處理一個客戶端的事件。
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.*;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
public class Netty_test {
public static void main(String[] args) {
ChannelFactory factory =new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),Executors.newFixedThreadPool(1));
ServerBootstrap bootstrap = new ServerBootstrap(factory);
ChannelPipelineFactory cpf=new ChannelPipelineFactory(){
public ChannelPipeline getPipeline() {
return Channels.pipeline(new testHandler());
}
};
bootstrap.setPipelineFactory(cpf);
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.bind(new InetSocketAddress(100));
}
}
class testHandler extends SimpleChannelHandler {
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
System.out.println("messageReceived, handler work.");
e.getChannel().write(e.getMessage());
ctx.sendUpstream(e);
}
}
但在我上面的例子中,服務器只能處理一個持久連接。 – Porcelain 2013-02-21 02:05:50
我已經在Win7和Linux(CentOS 5)中使用netty-3.2.4.Final.jar嘗試了這個例子。 – Porcelain 2013-02-21 02:06:51
你有更多的工作者比線程。將工廠更改爲1個工人,您會看到它可以處理多個連接。 – jdb 2013-02-21 02:30:30