Android Lunar Lander 线程处理替代方案

发布于 2024-10-26 03:25:26 字数 605 浏览 1 评论 0原文

和许多 Android 编程新手一样,我使用 Lunar Lander 作为实现 SurfaceView 的指南。我正在通过创建 PONG 版本来练习。代码的基本结构与LunarLander相同。显然,我很快就发现了月球着陆器中的错误。我解决这个问题的直接方法是在 SurfaceCreated() 中实例化一个新的 Thread 对象并在原始线程无法启动时启动它(顺便说一句,这与很多人建议的方法相同):

我的主要问题这实际上是不是一个好的做法?实例化一个新的线程对象意味着实例化游戏所需的所有内容,从而使所有先前实例化的数据挂起。如果你看看 LunarLander 本身,几乎游戏的每个核心组件都在线程中。我读过一些人们跑去

System.gc();

进行垃圾收集的帖子,但这通常被认为是不好的建议。

我正在尝试另一种解决方法,而不是在 SurfaceDestroyed() 中加入线程,而是简单地中断它。此外,当活动失去焦点时,我不会让 run() 返回,而是让它在后台完全不执行任何操作,而其他所有操作都暂停。我让 Activity 生命周期中的 onDestroy() 销毁所有内容。希望所有数据都不需要重新实例化,而旧数据则挂起。这是一个合适的替代方案吗?

提前致谢。

Like many of the novices of android programming, I used Lunar Lander as a guide for implementing the SurfaceView. I'm practicing by creating a version of PONG. The basic structure of the code is the same as LunarLander. Obviously, I soon discovered the bug that was in Lunar Lander. The immediate way I got around this is by instantiating a new Thread object in SurfaceCreated() and starting it, when the original thread can't be started (which incidentally, is the same method that a lot of people suggested):

My main question is whether this is actually good practice? Instantiating a new thread object would mean instantiating everything the game requires, thereby leaving all previously instantiated data hanging. If you look at LunarLander itself, almost every core component of the game is in the thread. I've read a few threads where people ran

System.gc();

to do a garbage collect, but that was generally thought to be bad advice.

I'm trying another workaround where instead of joining the thread in SurfaceDestroyed(), I simply interrupt it. Furthermore, when the activity loses focus, I don't let run() return, but cause it to do absolutely nothing in the background while everything else is paused. I let onDestroy() in the activity life cycle destroy everything. The hope is so that all the data don't need to be re-instantiated, while the old data left hanging. Can this be a suitable alternative?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戴着白色围巾的女孩 2024-11-02 03:25:26

只要在重新获得焦点时正确完成必要的同步,在失去焦点时保留线程对象当然是可能的。

但是,为了简单起见并在不处于焦点时释放资源,您可以将游戏状态提取到不属于游戏线程的类中,并在启动时将其传递给线程,从而允许您在 surfaceDestroyed() 中停止线程,如下所示在原始代码中完成。

显然,每次失去焦点时,线程对象都会变成垃圾,但收集这些微薄的字节应该不成问题。

Keeping the thread object while focus is lost is certainly possible provided the necessary synchronization is done correctly when you regain focus.

However, for simplicity's sake and to release resources when not in focus, you could extract the game state into a class not owned by the game thread and instead pass it to the thread when starting, allowing you to stop the thread in surfaceDestroyed() as done in the original code.

Obviously a thread object will be garbage for each time you loose focus, but the collection of these meager bytes shouldn't be a problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文