Java套接字支持全双工吗?

时间:2022-08-26 18:36:06

Is it possible to have one thread write to the OutputStream of a Java Socket, while another reads from the socket's InputStream, without the threads having to synchronize on the socket?

是否可以让一个线程写入Java套接字的OutputStream,而另一个线程从套接字的InputStream读取,而不需要线程在套接字上同步?

2 个解决方案

#1


46  

Sure. The exact situation you're describing shouldn't be a problem (reading and writing simultaneously).

确定。你所描述的确切情况不应该是一个问题(同时阅读和写作)。

Generally, the reading thread will block if there's nothing to read, and might timeout on the read operation if you've got a timeout specified.

一般情况下,读取线程会阻塞,如果没有什么可读的,如果指定了超时,则可能会超时读取操作。

Since the input stream and the output stream are separate objects within the Socket, the only thing you might concern yourself with is, what happens if you had 2 threads trying to read or write (two threads, same input/output stream) at the same time? The read/write methods of the InputStream/OutputStream classes are not synchronized. It is possible, however, that if you're using a sub-class of InputStream/OutputStream, that the reading/writing methods you're calling are synchronized. You can check the javadoc for whatever class/methods you're calling, and find that out pretty quick.

由于输入流和输出流在套接字中是独立的对象,所以您唯一可能关心的是,如果有两个线程同时尝试读或写(两个线程,相同的输入/输出流)会发生什么情况?InputStream/OutputStream类的读/写方法不同步。但是,如果您正在使用InputStream/OutputStream的子类,那么您正在调用的读/写方法可能是同步的。您可以检查javadoc以获取正在调用的任何类/方法,并很快发现这一点。

#2


9  

Yes, that's safe.

是的,这是安全的。

If you wanted more than one thread reading from the InputStream you would have to be more careful (assuming you are reading more than one byte at a time).

如果您希望从InputStream读取多个线程,那么您必须更加小心(假设您一次读取多个字节)。

#1


46  

Sure. The exact situation you're describing shouldn't be a problem (reading and writing simultaneously).

确定。你所描述的确切情况不应该是一个问题(同时阅读和写作)。

Generally, the reading thread will block if there's nothing to read, and might timeout on the read operation if you've got a timeout specified.

一般情况下,读取线程会阻塞,如果没有什么可读的,如果指定了超时,则可能会超时读取操作。

Since the input stream and the output stream are separate objects within the Socket, the only thing you might concern yourself with is, what happens if you had 2 threads trying to read or write (two threads, same input/output stream) at the same time? The read/write methods of the InputStream/OutputStream classes are not synchronized. It is possible, however, that if you're using a sub-class of InputStream/OutputStream, that the reading/writing methods you're calling are synchronized. You can check the javadoc for whatever class/methods you're calling, and find that out pretty quick.

由于输入流和输出流在套接字中是独立的对象,所以您唯一可能关心的是,如果有两个线程同时尝试读或写(两个线程,相同的输入/输出流)会发生什么情况?InputStream/OutputStream类的读/写方法不同步。但是,如果您正在使用InputStream/OutputStream的子类,那么您正在调用的读/写方法可能是同步的。您可以检查javadoc以获取正在调用的任何类/方法,并很快发现这一点。

#2


9  

Yes, that's safe.

是的,这是安全的。

If you wanted more than one thread reading from the InputStream you would have to be more careful (assuming you are reading more than one byte at a time).

如果您希望从InputStream读取多个线程,那么您必须更加小心(假设您一次读取多个字节)。