java - 等待线程不恢复

标签 java android multithreading

我一直在制作一个游戏,其中在屏幕上绘制了一个大小递减的圆圈,并且用一根手指按在屏幕上,你必须停留在这些圆圈上。如果你离开圈子,你就输了。

我试图让这个游戏暂停一个线程,并在游戏失败后停止时恢复它(我所在的线程似乎非常占用处理器,即使它什么都不做)。我在下面包含了一些代码,并提供了所有触摸事件方法。

有谁知道我哪里出错了?我查看了许多线程并尝试了多种不同的方法,但到目前为止都没有成功。

我省略了一些代码以尽量保持简洁。

public class MainActivity extends Activity {
...
Thread mainThread;

public void onCreate(Bundle savedInstanceState) {
    ...
    paused = true;

    mainThread = new Thread(new Runnable() {
        public void run() {
            synchronized (this) {
                if (paused) {
                    try {
                        paused = false;
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            while (true) {
                if (!paused) {
                    ... GAME STUFF ...
                }
            }
        }
    });
    mainThread.start();
}


public boolean onTouchEvent(MotionEvent e) {
    if (e.getPointerCount() == 1) {
        switch (e.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                if (alertDialog == null) {
                    go();
                } else if (alertDialog.isShowing() == false) {
                    go();
                }
                return true;

            case MotionEvent.ACTION_UP:
                pause();
                saveHighScore();
                dialog("You lifted your finger from the screen! \n" +
                        "You scored " + getScore() + ". \n" +
                        "Your highscore is " + getHighScore());
                reset();
                return true;

            case MotionEvent.ACTION_MOVE:
                if (alertDialog == null) {
                    go();
                } else if (alertDialog.isShowing() == false) {
                    if (!checkBounds(e.getX(), e.getY())) {
                        pause();
                        saveHighScore();
                        dialog("You moved outside a circle! \n" +
                                "You scored " + getScore() + ". \n" +
                                "Your high score is " + getHighScore());
                        reset();
                    }
                }

                return true;
        }
    } else {
        pause();
        saveHighScore();
        dialog("You can only use 1 finger at a time! \n" +
                "You scored " + getScore() + ". \n" +
                "Your high score is " + getHighScore());
        reset();
    }

    return true;
}

private synchronized void pause() {
    paused = true;
}

private synchronized void go() {
    if (mainThread.getState() == Thread.State.WAITING) {
        paused = false;
        notify();
    }
}

最佳答案

问题出在您的主线程中。它以暂停状态开始,一次将取消暂停,然后永远停留在

while (true) {
   if (!paused) {
      ... GAME STUFF ...
   }
}

如果 pause 再次变为 true,您将只是主动循环而无需执行 GAME STUFF。线程没有 hibernate (这就是为什么它什么都不做就非常活跃的原因),所以这个 if 语句永远不会执行

if (mainThread.getState() == Thread.State.WAITING) {
    paused = false;
    notify();
}

暂停将保持为真。

你可以这样修复:

mainThread = new Thread() {public void run() {

        while (true) {
            if (!paused) {
                //... GAME STUFF ...
            }
            else synchronized (this) {
                try {
                    paused = false;
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
};

此外,将 notify() 更改为:

synchronized (mainThread) {mainThread.notify();}

wait 和 notify 方法必须在同一个对象上调用(在您的线程内)

关于java - 等待线程不恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23620112/

相关文章:

java - android phonegap cordova webview 事件

android - 如何在android中处理首选项的点击事件

android - 为什么MQTT客户端无法重连?

java - 在 servlet 中使用 java.util.Timer 是否安全?

java - 使用 MultiActionController 进行 Spring-MVC 表单验证

java - 谷歌健身 : Cannot update both step and distance in real-time

java - setContextClassLoader 在并发调用时速度急剧下降

c++ - 为什么 std::async 比简单的分离线程慢?

java - 检查最终实例变量是否已初始化

java - java下载的区别