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.
1 2 3 4 5 6 7 8 9 10 11 12 13
| 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| 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); } private void run(){ beforeTime = System.nanoTime(); running = true; while(running){ ++numLoop; userInterface.update(); afterTime = System.nanoTime(); timeDiff = afterTime - beforeTime; sleepTime = (period - timeDiff) - overSleepTime; if(sleepTime > 0){ try{ Thread.sleep(sleepTime/2000000L); }catch(InterruptedException ex){} overSleepTime = (System.nanoTime() - afterTime) - sleepTime;
}else{ excess -= sleepTime; overSleepTime = 0L; if(++numberDelays >= NUM_DELAYS_PER_YIELD){ Thread.yield(); numberDelays = 0; } } beforeTime = System.nanoTime(); } } |