如何在JavaNIO中创建不同的选择器来接受新连接
我想用JavaNIO编写JavaTCP套接字编程。工作正常。但我使用相同的选择器来接受客户的读写 如何创建不同的选择器来接受JavaNIO中的新连接,读写。 有在线帮助吗 实际上,当我忙于阅读或写作时,我的选择器会使用更多的迭代器。所以,如果连接了更多的客户端,那么接受新连接的性能就会变慢。 但我不希望接受客户的速度太慢 //创建选择器并注册两个套接字通道 选择器=null; 试一试{ //创建选择器 选择器=selector.open() 谢谢 迪帕克有很多如何在JavaNIO中创建不同的选择器来接受新连接,java,nio,Java,Nio,我想用JavaNIO编写JavaTCP套接字编程。工作正常。但我使用相同的选择器来接受客户的读写 如何创建不同的选择器来接受JavaNIO中的新连接,读写。 有在线帮助吗 实际上,当我忙于阅读或写作时,我的选择器会使用更多的迭代器。所以,如果连接了更多的客户端,那么接受新连接的性能就会变慢。 但我不希望接受客户的速度太慢 //创建选择器并注册两个套接字通道 选择器=null; 试一试{ //创建选择器 选择器=selector.open() 谢谢 迪帕克有很多 如果您想创建一个新的选择器,请继续
如果您想创建一个新的选择器,请继续创建另一个选择器(与您复制的示例代码中的方法相同)。如果需要,您可以仅为连接操作向其注册频道(文档涵盖了这一点,但操作是OP_ACCEPT)。您可能希望有一个线程池来处理客户端处理工作—这样,主线程就可以将工作项排队,并立即开始接受侦听套接字上的新连接。每个选择器都需要自己的线程
如果您需要多个选择器,为什么不使用阻塞NIO呢?这会简单得多。非阻塞IO只有在您希望连接共享线程时才有意义。那么为什么不粘贴您实际使用的任何代码,而不是示例客户端代码呢?如果您接受连接,您必须在某个地方使用ServerSocketChannel。I没有任何迹象表明两个选择器不能在同一个线程上使用。这对我来说似乎很好(尽管出于我能想到的任何原因,两个选择器也不是必需的)。您可以在一个线程中使用任意数量的选择器。但是,我不明白这一点,即它给您提供了一个选择器无法提供的内容?听起来我们达成了一致。这只是“需要自己的线程”的措辞这让我很困惑。可能只有一个选择器专门处理接受操作,这样您就可以优先处理这些操作而不是读/写操作。我认为这是他所要求的,即使这没有意义,而且在没有额外工作的情况下也不会以他期望的方式实际提高性能。有一些有效的p在不同阶段使用多个选择器的模式。例如,一个选择器接受与另一个选择器的连接以进行活动读/写操作。有时,这使得处理SelectionKeys的“怪癖”变得更容易(例如,在多次调用select()时)因为工作线程还没有清理op,或者在non-select()中更改兴趣op的问题-调用threads.Grizzly做得很好。op应该在选择线程中清除,而不是在工作线程中清除。这只是一个麻烦的邀请。客户端被TCP堆栈预先接受。这就是积压队列的目的。它的性能不受迭代器性能的影响。您的问题似乎毫无意义。您可以很容易地增加积压工作,这就是您所需要的。
// Create two non-blocking sockets. This method is implemented in
// e173 Creating a Non-Blocking Socket.
SocketChannel sChannel1 = createSocketChannel("hostname.com", 80);
SocketChannel sChannel2 = createSocketChannel("hostname.com", 80);
// Register the channel with selector, listening for all events
sChannel1.register(selector, sChannel1.validOps());
sChannel2.register(selector, sChannel1.validOps());
} catch (IOException e) {
}
// Wait for events
while (true) {
try {
// Wait for an event
selector.select();
} catch (IOException e) {
// Handle error with selector
break;
}
// Get list of selection keys with pending events
Iterator it = selector.selectedKeys().iterator();
// Process each key at a time
while (it.hasNext()) {
// Get the selection key
SelectionKey selKey = (SelectionKey)it.next();
// Remove it from the list to indicate that it is being processed
it.remove();
try {
processSelectionKey(selKey);
} catch (IOException e) {
// Handle error with channel and unregister
selKey.cancel();
}
}
}
public void processSelectionKey(SelectionKey selKey) throws IOException {
// Since the ready operations are cumulative,
// need to check readiness for each operation
if (selKey.isValid() && selKey.isConnectable()) {
// Get channel with connection request
SocketChannel sChannel = (SocketChannel)selKey.channel();
boolean success = sChannel.finishConnect();
if (!success) {
// An error occurred; handle it
// Unregister the channel with this selector
selKey.cancel();
}
}
if (selKey.isValid() && selKey.isReadable()) {
// Get channel with bytes to read
SocketChannel sChannel = (SocketChannel)selKey.channel();
// See e174 Reading from a SocketChannel
}
if (selKey.isValid() && selKey.isWritable()) {
// Get channel that's ready for more bytes
SocketChannel sChannel = (SocketChannel)selKey.channel();
}
}