Java – how can I guarantee to send a “stay alive” heartbeat?
We have an RMI client application written in Java that needs to send "keep alive" messages to the server application regularly We have implemented it as a separate heartbeat thread, which sends the keep alive message to the server, and then uses thread Sleep() sleeps for 15 seconds
Set thread to high priority:
Thread heartbeatThread = new Thread(new HeartbeatRunnable(server)); heartbeatThread.setPriority(Thread.MAX_PRIORITY); heartbeatThread.start();
However, when the box running the client uses a lot of CPU, we find that the heartbeat is missed, which causes the server to think that our client application has died
We added thread. 0 to my main thread The yield () call, although this does not eliminate the problem
Is there any way to ensure that my heartbeat is sent on time while my application is still running?
Solution
You can implement user - mode threads in a non - threaded environment by freely distributing self - made "yield" functions in your code
Similarly, you can freely scatter heartbeat check function calls in your code Abandon the thread, just call the heartbeat function regularly to check whether the heartbeat needs to be sent
It's a rough solution, but since you've tried the right solution and it doesn't work, maybe it's something you have to go back to
In fact, what you can do is put a macro at the beginning of each function call, which will quickly check the time and call the heartbeat function if necessary
Ah, do you have Java macros? I don't think so - but you see