Created
December 28, 2011 19:01
-
-
Save robertgreiner/1529188 to your computer and use it in GitHub Desktop.
Roll a die
This file contains 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 RandomRoll; | |
import java.util.Random; | |
public class RandomRoll extends Thread { | |
private boolean running = true; | |
public RandomRoll() { | |
super("RandomRoll thread"); | |
} | |
public void run() { | |
while (running) { | |
System.out.println("Roll: " + rollDie()); | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
System.out.println("Sleep ERROR: " + e); | |
} | |
} | |
System.out.println("RandomRoll Thread Stopped"); | |
} | |
public void stopThread() { | |
System.out.println("Stopping RandomRoll Thread"); | |
this.running = false; | |
} | |
private final int rollDie() { | |
Random random = new Random(); | |
return random.nextInt(6) + 1; //1-6 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment