Thread.join () and Thread.interrupt () do not stop thread

I am having trouble stopping my threads.
I've tried calling both Thread.join()

, and Thread.interrupt()

alone and together, but I can't seem to get it to work.
I have a loop while

in each class that runs as long as the boolean is running

equal true

.
Then I stop the program by calling a method called stop

. The method stop

only sets to running

false, so the while loop ends.

EDIT Code:

public class Game implements Runnable {

    // The Thread
    private Thread thread;

    // The program running state
    private boolean running = false;

    // Create the Thread and the rest of the application
    public void create() {
        // Create the Thread
        thread = new Thread(this, "Game");
        // Start the Thread
        thread.start();
        // Set the program running state to true
        running = true;
    }

    public void run() {
        while(running) {
            // Render, update etc...
        }

        // When the while loop has exited
        // (when running is false (when the stop method has been called))
        try {
            // Join the thread
            thread.join();
            // Interrupt the thread
            thread.interrupt();
        } catch(InterruptedException e) {
            // Print the exception message
            e.printStackTrace();
        }
        // Exit the program
        System.exit(0);
    }

    // Stop the game
    public void stop() {
        // Set the program running state to false
        running = false;
    }

    // The main method
    public static void main(String[] args) {
        // Create a new instance of Game and start it
        new Game().create();
    }

      

+3


source to share


3 answers


A thread locks itself
When the termination process finishes, the thread ends, but you call a method join()

within a method run()

and that method waits for the thread to complete. This way the method run()

never ends and the thread never dies.
You did the right thing to implement some time-consuming boolean test that you can set in the method to terminate the thread.
To end the thread and wait for it to end, call the method join()

after the call stop()

.
System.exit(0)

in the startup method is not required. If your thread ends and the join () method returns, the main method ends, and so the program ends. I know your real program will be more complex, but you don't need to System.exit()

.

public void run() {
    while(running) {
        // Render, update etc...
    }
}

// The main method
public static void main(String[] args) {
    // Create a new instance of Game and start it
    Game game = new Game().create();
    game.stop();
}

      



EDIT 1:

// Stop the game
public void stop() {
    // Set the program running state to false
    running = false;
    game.interrupt();//Cause blocking methods like Thread.sleep() to end with an Interrupted exception
    game.join();//Wait until the thread is completed
}

      

+2


source


I believe you should rewrite your run () method:

public void run() {
    while(true) {
        // Render, update etc...

        // at the end, or after some steps, according to Java doc for Thread
        if (Thread.interrupted())  
           throw new InterruptedException();
    }
}

      

And your stop () method, by simply interrupting the thread, should do this:

public void stop() {
    thread.interrupt();  //use thread here, not game
}    

      

Add join () method:



public void join() throws InterruptedException {
    thread.join(); 
}    

      

So basically you are joining your topic

public static void main(String[] args) {
    // Create a new instance of Game and start it
    Game game = new Game();
    game.create();
    try {
        game.join();
    } catch (InterruptedException e) {
        //The interrupt should occur from another thread

        //Somebody called stop()
        //do your clean up
        System.exit(0);
    }           
}   

      

This is the associated JavaDoc .

What's not clear yet is exactly how you call stop ().

+1


source


From what I understand, you need to stop all your threads when your game exits main. You can set Daemon (true) to all of your threads and they will be automatically closed.

Read about setDaemon in the documentation http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setDaemon(boolean)

0


source







All Articles