等待线程没有恢复

时间:2014-05-12 22:41:39

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

1 个答案:

答案 0 :(得分:3)

问题在于你的主线程。它以暂停状态开始,将取消暂停,然后保持卡住

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

如果暂停再次成为现实,您将只是主动循环,而不会进行GAME STUFF。线程没有休眠(这就是为什么它没有做任何事情就非常活跃),所以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方法

相关问题