我可以同时写入和读取 SocketChannel 吗?

Can I write and read SocketChannel at once?

我打开了 java.nio.channels.SocketChannel 的对象 (socketChannel)。

socketChannel.configureBlocking(false);

安全吗:

  1. 从一个线程写入,
  2. 在另一个线程中读取它,

同时?

是的,可以让一个线程写入 SocketChannel,同时一个线程从中读取。

来自 Javadoc:

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.

https://docs.oracle.com/javase/8/docs/api/java/nio/channels/SocketChannel.html