Interruptible network I/O in Java

后端 未结 3 1078
再見小時候
再見小時候 2020-12-20 18:13

In Java 1.4+, there\'re 3 ways to interrupt a stream which is blocked on socket I/O:

  1. If the socket was created using a regular java.net.Socket(InetAddress, int
3条回答
  •  清歌不尽
    2020-12-20 18:49

    1) Since the underlying OS call error that rasies the exception comes from a TCP stack that is multithread aware, there should not be any problem.

    2) Not sure- have not tried it, but would be surprised if there was any issue.

    3) There may be some performance differences. An OS close() call requires a 4-way handshake with the TCP peer - not sure which OS support that in a non-blocking manner, (same with connect).

    4) Thread..or socket. You have to keep a reference to something. Since you are closing the socket, it seems reasonable to keep a reference to it :)

提交回复
热议问题