Don’t use the idea of fixed timeout. There is a dedicated thread to manage all threads and receive heartbeats. This thread naturally has the heartbeat information of all threads. It also naturally knows which thread has not received heartbeats for more than 5 seconds, and then sends messages to the threads that should be terminated in this thread. interrupt signal.
The simple way is to set the read timeout directly. The second is to use non-blocking IO to implement communication, so that the timeout program can be done directly in the current thread.
Don’t use the idea of fixed timeout.
There is a dedicated thread to manage all threads and receive heartbeats. This thread naturally has the heartbeat information of all threads. It also naturally knows which thread has not received heartbeats for more than 5 seconds, and then sends messages to the threads that should be terminated in this thread. interrupt signal.
The simple way is to set the read timeout directly. The second is to use non-blocking IO to implement communication, so that the timeout program can be done directly in the current thread.
Use netty for java communication, a choice you won’t regret