问题描述
这是我的情况:我正在构建一个Android游戏,我的游戏活动由一个自定义的SurfaceView组成,它有一个游戏逻辑和渲染的线程。 该架构类似于Google网站上的LunarLander演示。
当活动开始时,它会创建surfaceView并调用此方法:
@Override
public void surfaceCreated(SurfaceHolder holder)
{
renderThread.start();
}
当我按下主页按钮退出游戏时,会调用onPause()方法,该方法调用surfaceDestroyed()。 在surfaceDestroyed中,我通过调用来停止游戏Thread:
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
synchronized(holder)
{
renderThread.stop();
}
}
该应用程序的背景很好。 然后,当我通过按下图标重新启动应用程序时,我在日志中收到“Thread already started”消息以及屏幕上的“强制关闭”弹出窗口。 当活动在渲染线程上调用start()时进入“surfaceCreated”方法时,会出现此消息。
现在我已经研究了几个小时,无法弄清楚为什么会这样。 我相信当我关闭应用程序时我的线程已停止,所以我不明白为什么它说它已经开始了。
1楼
那些方法不符合你的想法。 从 :
不止一次启动线程永远不合法。
和:
public final void stop()
- 已弃用。 这种方法本质上是不安全的。
如果要暂停线程,则必须在线程内使用Object.wait()
和Objecft.notifyAll()
。
2楼
在我看来,如果你打算经常启动和停止线程,你不应该将你的代码打包到Thread的子类中(这些例子是因为它使代码更短)。 请改用Runnable。 这样你可以在需要时停止并丢弃旧的Thread,并在需要再次启动时创建一个新的Thread对象来执行Runnable。
private TutorialRunnable tutorialRunnable;
...
// Synchronization and error checking omitted for brevity.
public void surfaceCreated(SurfaceHolder holder) {
thread = new Thread(tutorialRunnable);
thread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
tutorialRunnable.setRunning(false);
while (thread != null) {
try {
thread.join();
thread = null;
} catch (InterruptedException e) {
}
}
}
此外,依赖异常是不好的形式。 当您自己的代码意外行为时,这应该用作最后的手段。
3楼
一个糟糕的解决方案但它有效..
public void surfaceCreated(SurfaceHolder holder) {
try{
_thread.setRunning(true);
_thread.start();
}catch(Exception ex){
_thread = new TutorialThread(getHolder(), this);
_thread.start();
}
}
惩教受到热烈欢迎。