是否可以在 Android 中恢复中断的 Thread

最佳答案

您不应该通过其 API 恢复 Thread,不推荐使用 resume() 方法( ojita )。
您可以通过杀死线程并启动一个新线程来模拟恢复线程:

/**
Since Thread can't be paused we have to simulate pausing.
We will create and start a new thread instead.
*/
public class ThreadManager
{
    private static GameThread gameThread = new GameThread();

    public static void setRunning(boolean isRunning)
    {
        if (isRunning)
        {
            gameThread = new GameThread();
            gameThread.setRunning(true);
            gameThread.start();
        }
        else
        {
            gameThread.setRunning(false);
        }
    }

    public static boolean isRunning()
    {
        return gameThread.isRunning();
    }

    public static void join() throws InterruptedException
    {
        gameThread.join();
    }
}

关于android - 恢复被中断的线程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15347572/

10-15 09:58