在NIO的线程实现中需要帮助

时间:2021-10-19 00:36:16

I want to have create NIOServer which reads data from client using 1 thread and writes data to the client using another thread. also accepting client connection will be in some other thread.

我想创建NIOServer,它使用1个线程从客户端读取数据,并使用另一个线程将数据写入客户端。也接受客户端连接将在其他一些线程中。

Is there any online help

有没有在线帮助

Thanks Deepak

3 个解决方案

#1


Why use so many threads, if you do all that more easily with a selector and a single thread? If you want a multithread approach to net I/O, then you should consider plain old, blocking sockets.

如果使用选择器和单个线程更轻松地完成所有操作,为什么要使用这么多线程?如果你想要一个多线程的网络I / O方法,那么你应该考虑普通的旧的阻塞套接字。

BTW, you can do all that with 3 selectors: one accept connections, one reads and one writes. When you accept a connection, you can then register the channel with the other two selectors, with the appropriate interest ops.

顺便说一句,你可以用3个选择器完成所有这些:一个接受连接,一个读取和一个写入。当您接受连接时,您可以使用其他两个选择器注册该通道,并使用适当的兴趣操作。

Of course, when you've to free the channel, you've to cancel the SelectionKey from both selectors (read and write) or close it.

当然,当你要释放频道时,你必须从两个选择器中取消SelectionKey(读取和写入)或关闭它。

#2


I suggest you have a look at the sample code which comes the JDK under sample/nio/server

我建议你看一下示例代码,它来自sample / nio / server下的JDK

#3


I would recommend you Apache Mina : very flexible, high level abstraction, high performance, etc.

我建议您使用Apache Mina:非常灵活,高级抽象,高性能等。

#1


Why use so many threads, if you do all that more easily with a selector and a single thread? If you want a multithread approach to net I/O, then you should consider plain old, blocking sockets.

如果使用选择器和单个线程更轻松地完成所有操作,为什么要使用这么多线程?如果你想要一个多线程的网络I / O方法,那么你应该考虑普通的旧的阻塞套接字。

BTW, you can do all that with 3 selectors: one accept connections, one reads and one writes. When you accept a connection, you can then register the channel with the other two selectors, with the appropriate interest ops.

顺便说一句,你可以用3个选择器完成所有这些:一个接受连接,一个读取和一个写入。当您接受连接时,您可以使用其他两个选择器注册该通道,并使用适当的兴趣操作。

Of course, when you've to free the channel, you've to cancel the SelectionKey from both selectors (read and write) or close it.

当然,当你要释放频道时,你必须从两个选择器中取消SelectionKey(读取和写入)或关闭它。

#2


I suggest you have a look at the sample code which comes the JDK under sample/nio/server

我建议你看一下示例代码,它来自sample / nio / server下的JDK

#3


I would recommend you Apache Mina : very flexible, high level abstraction, high performance, etc.

我建议您使用Apache Mina:非常灵活,高级抽象,高性能等。