Results 1 to 2 of 2
- 12-21-2009, 10:16 PM #1Member
- Join Date
- Dec 2009
- Rep Power
Timer won't execute untill daemon thread yields
In short I'm finding that a daemon thread appears to be preventing a timer from another thread from running until it completes it's startup process. But to give more detail...
I have a client application with a main class that has only the constructor and synchronized connect/disconnect methods. The constructor starts a timer which sends a heartbeat to my server once every second. I found my server is killing the client every time I connect because it's not receiving the heartbeat soon enough and thus assuming the server is dead/frozen.
I traced the problem to another thread started by my main threads connect method. If this thread isn't started there is no problem, when this thread starts everything is delayed.
After further testing I discovered that heartbeat messages will work fine until a connection, then the daemon thread would start running and no heartbeat messages are sent until the daemon thread finishes it's first iteration and calls sleep (it's an infinite loop that sleeps between polling). Once the daemon thread yields I see two heartbeats immediately sent, as if they were queued and waiting to be sent. The entire process takes about 2 seconds.
So why does a daemon thread prevent my timer from running? I would think that after a second the JVM would interrupt the daemon thread long enough for the timer to run, wouldn't it? Is it possible to set the timer thread as a higher priority so it is guaranteed to interrupt other threads at least once a second?
- 12-24-2009, 08:17 AM #2
It sounds like the daemon thread is simply not yielding during its initialization. Even if it gets preempted, your heartbeat thread could yield and the daemon thread could take over again until it is preempted.
My first solution would be to put sleep calls in the initialization, to ensure the daemon doesn't abuse the other threads. I doubt a sleep(5) placed strategically in the daemon initialization process will affect its performance.
- By neha_negi in forum Threads and SynchronizationReplies: 3Last Post: 09-04-2009, 02:45 AM
- By dawiz001 in forum Threads and SynchronizationReplies: 5Last Post: 03-07-2009, 09:25 PM
- By Goseph in forum Threads and SynchronizationReplies: 7Last Post: 02-16-2009, 11:22 AM
- By y0y in forum New To JavaReplies: 4Last Post: 02-05-2009, 07:06 PM
- By Java Tip in forum java.utilReplies: 0Last Post: 06-26-2008, 08:32 PM