Created
July 24, 2015 16:39
-
-
Save anonymous/51552c3aeaa1254c3e65 to your computer and use it in GitHub Desktop.
game loop that can bepaused / stopped.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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