简体   繁体   English

如何在一个线程中读取SocketChannel,并从n个线程写入?

[英]How to read SocketChannel in one thread, and write from n threads?

I have a socketChannel (java.nio.channels.SocketChannel) listening for reading requests (from multiple clients). 我有一个socketChannel (java.nio.channels.SocketChannel)监听读取请求(来自多个客户端)。 It stores each request in a Request Queue . 它将每个请求存储在请求队列中

Also socketChannel.configureBlocking(false) 还有socketChannel.configureBlocking(false)

Then I want the multiple threads to take one request at a time from the R equest Queue and write to the socketChannel 然后我希望多个线程一次从R equest Queue获取一个请求并写入socketChannel

I have read the following from a documentation. 我从文档中读到了以下内容。

Socket channels are safe for use by multiple concurrent threads. 套接字通道可以安全地由多个并发线程使用。 They support concurrent reading and writing, though at most one thread may be reading and at most one thread may be writing at any given time. 它们支持并发读写,但最多只有一个线程可能正在读取,并且最多一个线程可能在任何给定时间写入。

Since only 1 thread can be written, what can I do in the case of multiple writes? 由于只能写入1个线程,在多次写入的情况下我该怎么办?

You can use your own lock synchronized or ReentrantLock , or queue the messages and have one thread do the actual writes. 您可以使用自己的锁synchronizedReentrantLock ,或者对消息进行排队,让一个线程执行实际写入。

The problem with writes is you can only atomically write one byte at a time, if you write more than one byte, you might send some, but not all of the data in which case another thread can attempt to write it's message and you get a corrupted message. 写入的问题是你一次只能原子地写一个字节,如果你写了多个字节,你可能会发送一些但不是所有数据,在这种情况下,另一个线程可以尝试写入它的消息,你得到一个损坏的消息。

I have a java.nio.channels.SocketChannel listening for reading requests (from multiple clients). 我有一个java.nio.channels.SocketChannel侦听读取请求(来自多个客户端)。

No you don't. 不,你没有。 You might have a ServerSocketChannel that listens for connections from multiple clients, but once you have an accepted SocketChannel , it is only connected to one client. 您可能有一个ServerSocketChannel 侦听来自多个客户端的连接 ,但是一旦您拥有一个已接受的SocketChannel ,它就只连接到一个客户端。 All you can get from it is sequential requests from that client. 您可以从中获得所有来自该客户端的顺序请求。

It stores each request in a Request Queue. 它将每个请求存储在请求队列中。

I don't see any need for that. 我认为没有必要。

Also socketChannel.configureBlocking(false) 还有socketChannel.configureBlocking(false)

Then I want the multiple threads to take one request at a time from the Request Queue and write to the socketChannel 然后我希望多个线程一次从请求队列中获取一个请求并写入socketChannel

Why not just compute the reply as soon as you read it and write it directly back? 为什么不在阅读后立即计算回复并直接写回来?

I have read the following from a documentation. 我从文档中读到了以下内容。

Socket channels are safe for use by multiple concurrent threads. 套接字通道可以安全地由多个并发线程使用。 They support concurrent reading and writing, though at most one thread may be reading and at most one thread may be writing at any given time. 它们支持并发读写,但最多只有一个线程可能正在读取,并且最多一个线程可能在任何给定时间写入。

Since only 1 thread can be written, what can I do in the case of multiple writes? 由于只能写入1个线程,在多次写入的情况下我该怎么办?

What multiple writes? 什么多次写? You only have one client request per channel. 每个频道只有一个客户端请求。 You only need to write one reponse per request. 您只需要为每个请求写一个响应。 You should not read, let alone process, a new request until you've written the prior response. 在您编写先前的响应之前,您不应该阅读,更不用说处理新的请求了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM