3

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

4

2 回答 2

3

您不应该通过其 API 恢复 Thread,resume()方法已弃用(原因)。

您可以通过杀死线程并启动一个新线程来模拟恢复线程:

/**
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();
    }
}
于 2013-03-11T20:08:47.900 回答
0

Thread不支持这些操作,因为相关方法已被弃用。

于 2013-03-11T20:12:56.680 回答