Some questions about Socket read hanging?

there is a thread class (extends Thread) with the following code in the run method

while(true) {
    Socket socket = null;
    GZIPInputStream gzis = null;
    ObjectInputStream ois = null;
    try {
        socket = new Socket(IP, PORT);
        gzis = new GZIPInputStream(socket.getInputStream());
        ois = new ObjectInputStream(gzis);
        Map<Integer, Object> result = (Map<Integer, Object>) ois.readObject();
        doSomeBuziness(result);
    } catch(Exception e) {
        log.error(...);
    } finally {
        // clean work here
        sleep(1000);
    }
}

that is, after the thread starts, it receives data from the Socket server every 1 second and then does some business logic processing on the received data

suddenly found that the data has not been updated. The jstack command shows that the thread has been suspended

.
"Thread-5" daemon prio=10 tid=0x00002b54800cd000 nid=0x691f runnable [0x00002b5433be7000]
   java.lang.Thread.State: RUNNABLE
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:152)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:238)
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
    at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:116)

ask:

  • Why are all threads suspended or RUNNABLE state? Not BLOCKED status?
    • what is the underlying mechanism for suspending? For example, what is the difference between the corresponding system command and waiting for the lock release to cause the thread to hang (what is the difference in the underlying mechanism)
static synchronized void foo() {
    Thread.sleep(10*60*1000);
}    

if there is a foo method with a synchronization block, both threads call the foo method, and one of them will be blocked

"pool-1-thread-2" prio=5 tid=0x00007f9ed00a5000 nid=0x5503 waiting for monitor entry [0x00007000079c7000]
   java.lang.Thread.State: BLOCKED (on object monitor)
    at com.demo.LockDemo.foo(LockDemo.java:24)
    - waiting to lock <0x00000007aae446d0> (a java.lang.Class for com.demo.LockDemo)
    at com.demo.LockDemo$1.run(LockDemo.java:15)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
  • for this suspension of Socket read , I don"t want to restart the application. Is there any way to explicitly end this suspension manually?
    at java.net.SocketInputStream.socketRead0(Native Method)

if you kill the socket explicitly through the command line, you can throw an exception and then sleep for a second and then go on without getting stuck

Jun.29,2022

you should have JDK documentation on thread status java.lang.Thread.State


socket ,

socket = new Socket(IP, PORT);
socket.setSoTimeout(1000 * 60);  //  60 

in this case, when socket read does not receive data for more than 60 seconds, a SocketException exception will be thrown.

in addition, resources should be released (close socket) in try finally .

Menu