search

Home  >  Q&A  >  body text

Java:底端上送心跳时线程继续执行,若超过5秒未上送心跳则线程中断

不用贴代码,赐我点儿思路就可以,我只会设置线程超时,但是底端上送心跳时这个线程应该是不受影响继续执行的,设置了超时时间,就算心跳上来了到了超时时间这个线程也没了,比较苦恼=。=

或者是否可以更新线程超时时间呢?每次心跳过来我都重新设置一个线程的超时时间

PHP中文网PHP中文网2889 days ago361

reply all(3)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:18:17

    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.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:18:17

    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.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 09:18:17

    Use netty for java communication, a choice you won’t regret

    reply
    0
  • Cancelreply