How can the Java client Socket receive a message from the server under a blocking thread?

recently encountered a problem while writing the Socket client
the client created two threads
one listening keyboard input event, which uses buffered, to send the write stream to the server when the input is detected to be complete.

            String content = "";
            while (!(content = bufferedReader.readLine()).equals("exit")) {
                byte[] type= new byte[1];
                type[0] = Const.Message;
                byte[] bytes = EncodeKit.encode(type, content);
                outputStream.write(bytes);
                outputStream.flush();
            }

A while loop keeps sending heartbeats
A while loop receives messages from the server
the problem now is that I want to know at the client whether the time for the server to return the message has exceeded the specified time, and if so, it is determined that the connection has been disconnected.

The

question is that the thread that receives the message is separate from the thread that sends the heartbeat. How do I know if the
message returned by the server exceeds the specified time when this message is sent?
I always feel that there is something wrong with my design, but I don"t know how to implement it.

The reason why

ps: uses three threads is that the thread read method is a blocking method. If you write another method to receive a message in the while loop that sends the heartbeat, then
will always execute this method of receiving the message, and there is no way to send the heartbeat.
send heartbeat code:

            outputStream = socket.getOutputStream();
            inputStream = socket.getInputStream();
            while (true) {
                Thread.sleep(time);//
                byte[] type = new byte[1];
                type[0] = Const.HEART;
                byte[] bytes = EncodeKit.encode(type, "");
                //
                outputStream.write(bytes);
                outputStream.flush();
May.26,2021
Menu