Strange Loop Slow Down

Hi im having some trouble with slow down in a program im developing, below is the main driving loop. The slow down happens periodically and stalls the whole program even the awt event thread so i cant killl the program while its stalled. Im wondering if its to do with some thread issue i dont know about in xith??

As you can see below the xith rendering is done in another object (The UserInterface object ), which is a singleton e.g.


public class SingletonExample{

  private static SingletonExample sinEx = new SingletonExample();

  private SingletonExample(){
    
  }

  public SingletonExample getReference(){
    return sinEx;
  }

}

Ive printed messages at key points to try and track down the stalling point and it seems to be where the loop sleeps, but the loop sleeps for way longer than its suppossed to, as in its told to sleep for 33 milliseconds and the delay can sometimes be as long as 30 seconds???

Could the fact that the rendering is being done in another object be the cause or am i just barking up the wrong tree here???

Thanks for any help



private EmergeAnt(){
            
            System.out.println("EmergeAnt: Brian Heylin ITCarlow 2004");
            
    setFramePerSecond(30);
    
            userInterface = UserInterface.getReference();
            
    run();
    
    System.out.println("Loop Variables");
    System.out.println("Number Loops = " + numLoop);
    System.out.println("Number Sleeps = " + numSleep);
    System.out.println("Number Overworked = " + numOverWorked);
    System.out.println("Number Yields = " + numYield);
            
      }// end constructor EmergeAnt()
      
  /** Contains the driver loop 
   *  This loop dynamically adjusts itself for the best performance without
   *  comprimising the UserInterface update rate
   */
  private void run(){
    
    beforeTime = System.nanoTime(); // get absolute time before loop runs
    running = true;
    
    while(running){
      
      ++numLoop;
      /** perform updating of ai and ui etc. here*/
      userInterface.update();
   
      afterTime = System.nanoTime(); // get absolute time after udating finished
      timeDiff = afterTime - beforeTime;
      // check if the time available (period) was enough for the workload to complete
      sleepTime = (period - timeDiff) - overSleepTime;
       
      //  if the workload was low, we can afford to sleep and let other threads 
      //  execute
      if(sleepTime > 0){
               
        try{
          Thread.sleep(sleepTime/2000000L); // nano -> milli
        }catch(InterruptedException ex){}
        
        // due to sleep() method tendancy to sleep over the requested time
        // calc the acual time slept and compensate on next loop 
        overSleepTime = (System.nanoTime() - afterTime) - sleepTime;

      }else{ // The workload was too much for period
        
        excess -= sleepTime;
        overSleepTime = 0L;
        
        if(++numberDelays >= NUM_DELAYS_PER_YIELD){
          Thread.yield();
          numberDelays = 0;
        }
        
      }// end sleepTime check
      
      beforeTime = System.nanoTime(); // update the time
    }
  }// end run()