i’m trying to understand it broum, I used interupt and this is the code i got now, and… it works 90% of the time.
See the System output at the end of this post.
public synchronized void startGame() {
eventHandler.fireGameEvent(GameEvent.Status.RUNNING);
if( pauzed ) {
pauzed = false;
}
if( GameThread == null) {
createThread(ThreadNumber++);
}
else{
if( GameThread.isInterrupted() ) {
createThread(ThreadNumber++);
}
else
System.out.println("GameThread " + GameThread.getName() +" is not interupted? yet..." );
}
}
private void createThread(int threatNumb) {
/* Initialize the game thread. */
GameThread = new GameThread();
GameThread.setName("GameThread " + threatNumb);
GameThread.start();
System.out.println(GameThread.getName() + " Started");
}
private class GameThread extends Thread {
public void run() {
System.out.println("starting game.runGame()");
runGame();
}
}
Note: skipped logic
public void runGame() {
boolean gameRunning = true;
while (gameRunning) {
/* Exits the game,
* Placed above the other functions Because we need to draw one last String */
if( quit ) {
gameRunning = false;
eventHandler.fireGameEvent(GameEvent.Status.END);
}
// sleep
try {
Thread.sleep(sleepDelay);
} catch (InterruptedException e) {
System.out.println("InterruptedException: " + e.getMessage());
gameRunning = false;
}
}
System.out.println("This Game has ended!");
}
public void restart() {
System.out.println("RESTARTED!");
if( GameThread != null && !pauzed) {
GameThread.interrupt();
startGame();
}
else
System.out.println("Cannot Restart!");
}
The console prints:
GameThread 15 Started InterruptedException: sleep interrupted This Game has ended! starting game.runGame() Create 2 Random Blocks: New Random Block 5 Starts at java.awt.Point[x=5,y=0] New Random Block 2 Starts at java.awt.Point[x=3,y=0] RESTARTED! GameThread 16 Started InterruptedException: sleep interrupted This Game has ended! starting game.runGame() Create 2 Random Blocks: New Random Block 7 Starts at java.awt.Point[x=4,y=0] New Random Block 7 Starts at java.awt.Point[x=7,y=0] RESTARTED! GameThread 17 Started InterruptedException: sleep interrupted This Game has ended! starting game.runGame() Create 2 Random Blocks: New Random Block 6 Starts at java.awt.Point[x=5,y=0] New Random Block 6 Starts at java.awt.Point[x=4,y=0] RESTARTED! InterruptedException: sleep interrupted This Game has ended! [b]GameThread GameThread 17 is not interupted? yet...[/b]
if I use isalive in
if( GameThread.isInterrupted() ) {
createThread(ThreadNumber++);
}
then I have to push 2 times before it detects that it is not alive anymore.
GameThread 0 Started starting game.runGame() Create 2 Random Blocks: New Random Block 5 Starts at java.awt.Point[x=5,y=0] New Random Block 5 Starts at java.awt.Point[x=5,y=0] RESTARTED! [b]GameThread GameThread 0 is still alive. InterruptedException: sleep interrupted[/b] This Game has ended! RESTARTED! GameThread 1 Started starting game.runGame() Create 2 Random Blocks: New Random Block 3 Starts at java.awt.Point[x=5,y=0] New Random Block 7 Starts at java.awt.Point[x=2,y=0] RESTARTED! GameThread GameThread 1 is still alive. InterruptedException: sleep interrupted This Game has ended! RESTARTED! GameThread 2 Started starting game.runGame() Create 2 Random Blocks: New Random Block 5 Starts at java.awt.Point[x=5,y=0] New Random Block 4 Starts at java.awt.Point[x=4,y=0] RESTARTED! GameThread GameThread 2 is still alive. InterruptedException: sleep interrupted This Game has ended!