简体   繁体   English

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

[英]Thread.join() and Thread.interrupt() doesn't stop the thread

I'm having some issues with stopping my threads. 我在阻止我的线程方面遇到了一些问题。
I've tried calling both Thread.join() and Thread.interrupt() , alone and together, but I can't get it working. 我试过单独和一起调用Thread.join()Thread.interrupt() ,但是我无法让它工作。
I have a while loop in each class, which runs as long as a boolean called running equals to true . 我在每个类中都有一个while循环,只要一个名为running的boolean等于true running
I then stop the program by calling a method called stop . 然后我通过调用一个名为stop的方法来stop The stop method only sets running to false, so that the while loop exits. stop方法仅将running设置为false,以便while循环退出。

EDIT Code: 编辑代码:

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

The thread blocks itself 线程阻塞自己
A thread ends, when the run method is finished, but you call the join() method inside the run() method and this method waits until the thread is finished. 当run方法完成时,一个线程结束,但是你在run()方法中调用了join() run()方法,这个方法一直等到线程完成。 So the run() method never ends and the thread will never die. 所以run()方法永远不会结束,线程永远不会死。
You have done correct, to implement a while, which tests a boolean you can set in a method to end the thread. 你已经做了正确的,实现了一段时间,它测试了你可以在方法中设置的布尔结束线程。
To end the thread and waiting on its end, call the join() method after calling the stop() . 要结束线程并等待其结束,请在调用stop()之后调用join()方法。
The System.exit(0) in the run method is not needed. 不需要run方法中的System.exit(0) If your thread ends and the join() method returns, the main method ends and so the program is finished.I know, your real program will be more complex, but you don't need System.exit() . 如果您的线程结束并且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();
}

EDIT 1: 编辑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
}

I believe you should rewrite your run() method: 我相信你应该重写你的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();
    }
}

And your stop() method, simply interrupting the thread should do it: 而你的stop()方法,只是中断线程应该这样做:

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

Add a join() method: 添加join()方法:

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

So in your main, you join your thread 所以在你的主要内容中,你加入了你的主题

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 related JavaDoc . 这是相关的JavaDoc

What is not clear to me yet, is how exactly you invoke stop(). 我还不清楚的是你究竟是如何调用stop()的。

As far as I understand you need to stop all your threads when your game exits from main. 据我了解,当你的游戏退出主游戏时,你需要停止所有线程。 You can setDaemon(true) to all your threads and they will be closed automatically. 您可以将daDmonmon(true)设置为所有线程,它们将自动关闭。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM