开发者

DataInputStream readByte blocked / hanging

I'm looking at the code for a server that creates 开发者_如何学JAVAa thread to handle each incoming connection. The problem is that for some reason on a bunch of threads, the DataInputStream created from the socket is hanging on readByte and not throwing any exceptions. The timeout is set for 60 seconds, so I'm not sure what the next step is with this.

socket.setSoTimeout(timeout);
socketInputStream = socket.getInputStream();
byte connectionOptions = socketDataInputStream.readByte();


You indicated in a comment that you're waiting for a SocketException. If so you need to catch SocketTimeoutException instead. For example, this code will output timeout! if you telnet to port 3434 and wait 3 seconds:

try {
    ServerSocket ss = new ServerSocket(3434);
    Socket socket = ss.accept();
    socket.setSoTimeout(3000);
    InputStream socketInputStream = socket.getInputStream();
    DataInputStream dataInputStream = new DataInputStream(socketInputStream);
    dataInputStream.readByte();
} catch (SocketTimeoutException e) {
    System.out.println("timeout!");
}       


Check the full thread dump for deadlock.
ctrl + break for windows.
ctrl + | or kill -3 PID for linux.

or Use jvisualvm or jconsole tools.

timeout value should be in milliseconds.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜