0

In my pong game every object(ball and two paddles) is running in the independent thread.

static Ball b = new Ball(195, 145);

Thread ball = new Thread(b);
Thread paddle1 = new Thread(b.paddle1);
Thread paddle2 = new Thread(b.paddle2);
public void startGame(){
    gameStarted = true;
    ball.start();
    paddle1.start();
    paddle2.start();
}

I want to set game on pause when I press ESC and when I press ESC again - continue game. So in keyPressed event I've done like this

 if (e.getKeyCode() == KeyEvent.VK_ESCAPE){
            if (gameStarted) {
                gameStarted = false;
                ballCurrentX = b.x; //save all states
                ballCurrentY = b.y;
                ballXDirection = b.xDirection;
                ballYDirection = b.yDirection;
                p1Score = b.p1Score;
                p2Score = b.p2Score;
                p1CurrentY = b.paddle1.y;
                p2CurrentY = b.paddle2.y;
                try {
                    ball.interrupt();
                    ball.join();
                    paddle1.interrupt();
                    paddle1.join();
                    paddle2.interrupt();
                    paddle2.join();
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }   
            }
            else {
                gameStarted = true;
                continueGame();
            }
        } 

To continue game - I restart all threads but set parameters for the objects from previous game state

public void continueGame(){
    gameStarted = true;
    b = new Ball(ballCurrentX, ballCurrentY);
    b.xDirection = ballXDirection;
    b.yDirection = ballYDirection;
    b.p1Score = p1Score;
    b.p2Score = p2Score;
    b.paddle1.y = p1CurrentY;
    b.paddle2.y = p2CurrentY;
    ball.start();
    paddle1.start();
    paddle2.start();
}

But program throws IllegalThreadStateException and the game doesn't continue. What's the problem? It doesn't stop threads?

lapots
  • 12,553
  • 32
  • 121
  • 242
  • Why the need to use three separate threads? – Simon Forsberg Feb 10 '13 at 12:36
  • Because I want to have independent control of all objects. – lapots Feb 10 '13 at 12:40
  • What if you would have 200 game objects, would you then use 200 threads? What is stopping you from controlling more than one object from one thread? Maybe I don't understand what you mean by `independent control of all objects`. – Simon Forsberg Feb 10 '13 at 12:43
  • Because I don't know how. Every my object implements `Runnable` and has his own `draw` and `move` method. – lapots Feb 10 '13 at 12:56

3 Answers3

3

You cannot restart a thread after it was stopped/interrupted. But nothing stops you from creating a new thread and starting it.

A SO answer that lists some options you could use

Community
  • 1
  • 1
Denis Rosca
  • 3,409
  • 19
  • 38
3

To continue game - I restart all threads ...

You cannot restart a Java thread. The javadoc for Thread.start() states this:

" It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution."

"Throws: IllegalThreadStateException - if the thread was already started."

You need to either create new thread objects and start them, or figure out some way to tell the existing threads to enter an application-defined "paused" state when they are interrupted, and wait there until they are told to "resume".

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Even when I restart thread in continueGame `ball = new Thread(b); ball.start();` it still throws `IllegalThreadStateException`. – lapots Feb 10 '13 at 12:46
  • I expect that you aren't doing it right. The exception is only thrown if you have **previously** started the thread. – Stephen C Feb 10 '13 at 12:48
  • I start thread when I press button `startGame` - `ball.start()`. When I press ESC - `ball.interrupt(); ball.join();`. And when I press ESC again - `ball = new Thread(b); ball.start();`. Also I've got `Thread.sleep(8)` in `b` object(it implements `Runnable`). And that is all. – lapots Feb 10 '13 at 12:51
  • If calling `start()` on a Thread that hasn't been started before really throws an `IllegalThreadStateException`, then you have probably found a JVM bug. But I'll only believe this if you can provide clear evidence in the form of an SSCCE. – Stephen C Feb 10 '13 at 13:22
1

Threads are not allowed to be started again after being terminated. From the documentation:

"It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution."

Creating a new instance of the thread and starting it is legal. However, this won't achieve the "pause" effect, unless you make sure the state provided to the new thread represents accurately the paused state.

Eyal Schneider
  • 22,166
  • 5
  • 47
  • 78