1

小白不懂就问, Java InputStream 如何在接收数据时收到事件通知

 3 years ago
source link: https://www.v2ex.com/t/798400
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Java

小白不懂就问, Java InputStream 如何在接收数据时收到事件通知

  teliang · 6 小时 28 分钟前 · 407 次点击
各位大佬,现在有个场景是这样的:
我本地建立一个 Socket 连接到服务器,OutputStream.write 和 flush 给服务器发送数据,等待服务响应然后读取数据。

问题:
我 flush 后直接 InputStream.available()返回 0,需要 sleep 零点几秒才能拿到数据,并发请求的时候都 sleep 感觉不太好

请问:
InputStream 有没有事件通知呢,比如服务器返回数据后有个事件通知,我用线程池处理数据

跪求各位大佬指教

第 1 条附言  ·  2 小时 43 分钟前

5 楼说的对,我直接 read 就行,是我傻了,nio 我还不太会用,等我看看书再试试

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK