Skip to content

Instantly share code, notes, and snippets.

@robertgreiner
Created December 28, 2011 19:01
Show Gist options
  • Save robertgreiner/1529188 to your computer and use it in GitHub Desktop.
Save robertgreiner/1529188 to your computer and use it in GitHub Desktop.
Roll a die
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