Results 1 to 2 of 2
  1. #1
    dsollen is offline Member
    Join Date
    Dec 2009
    Rep Power

    Default 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?

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Rep Power


    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.

Similar Threads

  1. Inside a Timer thread loop,how to refresh a JTable in swing
    By neha_negi in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-04-2009, 02:45 AM
  2. Thread vs Timer
    By dawiz001 in forum Threads and Synchronization
    Replies: 5
    Last Post: 03-07-2009, 09:25 PM
  3. [SOLVED] should I run my Timer as a daemon?
    By Goseph in forum Threads and Synchronization
    Replies: 7
    Last Post: 02-16-2009, 11:22 AM
  4. Multithreaded daemon? High level help needed.
    By y0y in forum New To Java
    Replies: 4
    Last Post: 02-05-2009, 07:06 PM
  5. Replies: 0
    Last Post: 06-26-2008, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts