Android的月球着陆器线程处理的替代品 [英] Android Lunar Lander thread handling alternatives

查看:96
本文介绍了Android的月球着陆器线程处理的替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像许多机器人编程的新手,我用月球着陆作为实施SurfaceView的指南。我通过创建一个版本PONG的练习。的code中的基本结构是一样的LunarLander。很显然,我很快就发现,在月球着陆的bug。我得到了解决这个问题的直接方法是通过实例化在SurfaceCreated(新的Thread对象),并启动它,当原来的线程无法启动(顺便说一句,同样的方法,很多人建议):

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):

我的主要问题是,这是否是真正好的做法呢?实例化一个新的线程对象将意味着实例化一切的游戏需要,从而使所有的previously实例化的数据悬挂。如果你看一下LunarLander本身,几乎每一个核心的游戏组件是在线程。我读过几个线程里的人跑了。

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.

我想另外一个解决办法,而不是凡在SurfaceDestroyed()加入线程,我只是打断了。此外,在活动失去焦点,我不要让run()的回报,但导致它什么都不做,在后台,而其他一切都暂停。我让的onDestroy()中的活性生命周期摧毁一切。希望是,使得所有的数据不需要被重新实例化,而旧数据左悬挂。可这是一个合适的选择?

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?

在此先感谢。

推荐答案

保持线程对象,而焦点将丢失当然是可能的提供,当你重新集中注意力的必要同步正确。

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

但是,为了简单起见,并释放资源时,没有焦点,您可以汲取的比赛状态变成不被游戏线程所拥有的一类,而是它启动时传递给线程,让你停止线程surfaceDestroyed ()为已完成在原来的code。

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.

这篇关于Android的月球着陆器线程处理的替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆