Java 套接字是否支持全双工? [英] Do Java sockets support full duplex?

查看:26
本文介绍了Java 套接字是否支持全双工?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

好的.您所描述的确切情况应该不成问题(同时读取和写入).

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.

由于输入流和输出流是 Socket 中的独立对象,您唯一可能关心的是,如果您有 2 个线程尝试读取或写入(两个线程,相同的输入/输出流)会发生什么同时?InputStream/OutputStream 类的读/写方法不同步.但是,如果您使用 InputStream/OutputStream 的子类,则您调用的读/写方法可能是同步的.您可以查看 javadoc 以了解您正在调用的任何类/方法,并很快找到.

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.

这篇关于Java 套接字是否支持全双工?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆