Game loop eating 100% cpu

This is the base game loop I’m using for a game for J4K 2013. But it is eating 100% of my CPU. I’m running on Ubuntu 32 bit and Java 6. Any tips how can I avoid eating 100% of CPU? Thanks!


		int FPS = 60;
		int MILI_FRAME_LENGTH = 1000 / FPS;
		int frameCount = 0;

		while (true) {
			long startTime = System.currentTimeMillis();

			long lastLoopTime = System.currentTimeMillis();
			long lastFpsTime = 0;
			long fps = 0;
			while (true) {
				long delta = System.currentTimeMillis() - lastLoopTime;
				lastLoopTime = System.currentTimeMillis();
				lastFpsTime += delta;
				fps++;

				// If more than 1000ms (1s) has passed reset the fps count.
				if (lastFpsTime >= 1000) {
					lastFpsTime = 0;
					fps = 0;
				}

				logic(delta);
				render(g);

				frameCount++;
				while ((System.currentTimeMillis() - startTime)
						/ MILI_FRAME_LENGTH < frameCount) {
					Thread.yield();
				}

				// Ensures that the display is up-to-date. This keeps the
				// animation smoother for the Linux OS.
				Toolkit.getDefaultToolkit().sync();
			}
		}


Thread.yield only temporarily pauses the the thread and allows others to run. Since you don’t have another thread, it has no effect.
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html

Looks like you need to learn about game loops…

Here you go!

Thanks, I took this game loop from an older entry of the 4k competition but maybe I have modified it and removed the sleep statement. Thanks for pointing it out. Now my cpu rate is ok and my game still smooth.