Thread.join()和Thread.interrupt()不会停止线程

时间:2014-12-14 13:24:16

标签: java multithreading loops

我在停止线程方面遇到了一些问题 我已尝试单独和同时拨打Thread.join()Thread.interrupt(),但我无法让它工作。
我在每个类中都有一个while循环,只要名为running的布尔值等于true,就会运行。{ 然后我通过调用名为stop的方法来停止程序。 stop方法仅将running设置为false,以便while循环退出。

编辑代码:

public class Game implements Runnable {

    // The Thread
    private Thread thread;

    // The program's 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's 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's 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 个答案:

答案 0 :(得分:2)

线程阻止自己
当run方法完成时,线程结束,但是在join()方法中调用run()方法,此方法将等待,直到线程完成。所以run()方法永远不会结束,线程永远不会死 你已经做了正确的,实现了一段时间,它测试了一个你可以在方法中设置的布尔结束线程 要结束线程并等待结束,请在致电join()后调用stop()方法 不需要run方法中的System.exit(0)。如果您的线程结束并且join()方法返回,则main方法结束,因此程序结束。我知道,您的真实程序将更复杂,但您不需要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();
}

编辑1:

// Stop the game
public void stop() {
    // Set the program's 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
}

答案 1 :(得分:1)

我相信你应该重写run()方法:

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();
    }
}

你的stop()方法,只需中断线程即可:

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

添加join()方法:

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

所以在你的主要内容中,你加入了你的主题

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);
    }           
}   

这是相关的JavaDoc

我还不清楚,你究竟是如何调用stop()。

答案 2 :(得分:0)

据我所知,你需要在游戏退出主游戏时停止所有线程。 您可以将Daemon(true)设置为所有线程,它们将自动关闭。

阅读文档中的setDaemon http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setDaemon(boolean)