Skip to content

Instantly share code, notes, and snippets.

Created July 24, 2015 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/51552c3aeaa1254c3e65 to your computer and use it in GitHub Desktop.
Save anonymous/51552c3aeaa1254c3e65 to your computer and use it in GitHub Desktop.
game loop that can bepaused / stopped.
package com.example.foo;
public class SomeClass {
public static void main(final String[] args) {
final Thread thread = new Thread(SomeClass::gameLoop);
thread.start();
new Thread(() -> {
try {
Thread.sleep(2000);
} catch (final Exception e) {
e.printStackTrace();
}
SomeClass.running = false;
}).start();
}
static volatile boolean running = true;
private static void gameLoop() {
if (!running) {
System.out.println("Good Bye");
return;
}
try {
Thread.sleep(500);
} catch (final InterruptedException e) {
return;
}
Thread.yield();
System.out.println("pong");
gameLoop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment