Android 系统如何处理休眠时间过长的线程?
因此,在 Android 应用程序的“Home”活动中,我有一个在后台不断运行的非守护线程,它每 30 分钟左右检查一次博客中是否有新帖子,以触发通知。当我出于测试目的让线程休眠几秒钟以确保它确实在后台工作时,它工作得很好,但在实际的手机中,当事件应该在半小时后触发时,它只是没有't。所以我的问题是,当我执行 *Thread.sleep(30_MINUTES)* 时,Android 系统是否会杀死它或因处于不活动线程太长时间而杀死它? android 会杀死休眠线程吗?
So in my "Home" activity in my Android app, I have a non-deamon thread that is constantly running in the background that checks a blog for new posts every 30 minutes or so to fire notifications. It works fine when I make the thread sleep just for a few seconds for testing purposes to make sure that it is indeed working in the background, but in the actual phone, when the event is supposed to fire after half an hour, it just doesn't. So my question is, when I do *Thread.sleep(30_MINUTES)* does the android system kill it or something for being an inactive thread for too long? Does android kill sleepy threads?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不尝试 AlarmManager 每 30 分钟触发一次轮询代码?投票完成后,重新安排接下来的 30 分钟。 AlarmManager 比让线程休眠可靠得多
Why don't you try AlarmManager to fire your polling code every 30 minutes ? Once the polling is done re-schedule if for the next 30 minutes. AlarmManager is much more reliable than making the threads to sleep
好的,这样问题就解决了。导致问题的原因是,在 Android 中,当您将线程置于睡眠状态(比如说 30 分钟)时,当设备进入睡眠状态(手机不活动后屏幕会关闭)时,CPU 会进入所谓的“深度睡眠状态” “这会导致线程的计数器(唤醒线程)在屏幕关闭时暂停。这意味着,例如,当您说 Thread.sleep(500000) 时,仅当手机唤醒时才会计算这 500000 毫秒。解决方案是使用 AlarmManager 和 BroadcastReceiver 定期运行代码。
以下是有关如何将 AlarmManager 与 BroadcastReceiver 结合使用的简单指南:http://justcallmebrian.com/?p=129< /a>
OK, so the problem was solved. What was causing the problem was that in Android when you put a Thread to sleep for let's say 30 minutes, when the device goes to sleep (the screen turns off after inactivity on the phone) the CPU goes into a so called "deep sleep state" which causes the thread's counter -that wakes the thread wake up- to pause while the screen is off. This means that when you say Thread.sleep(500000) for example, those 500000 milliseconds are counted only when the phone is awake. The solution is to use AlarmManager and a BroadcastReceiver to run the code periodically.
Here's a simple guide on how to use AlarmManager with a BroadcastReceiver: http://justcallmebrian.com/?p=129
我不确定你在编码中做了什么,但Android系统绝对不会自行杀死线程,无论困与否都没关系。
I am not sure what you are doing in your coding but definitely android system does not kill a thread on its own whether sleepy or not doesnt matter.
这段时间到底发生了什么?我相信您所描述的内容(使用您所描述的技术)能够可靠工作的唯一情况是,如果(a)您的活动(或在同一进程中运行的另一个活动或服务)是整个进程的前台活动线程的生命周期以及 (b) 有一个唤醒锁阻止 CPU 休眠。
Android 可能不会杀死单个线程,但它绝对可以并且将会杀死基于 进程生命周期。
What exactly is happening during this time? I believe the only situation in which what you're describing (using the technique you describe) would work reliably is if both (a) your activity (or another activity or service that runs in the same process) is the foreground activity for the entire life of the thread and (b) there's a wakelock preventing the CPU from sleeping.
Android may not kill individual threads, but it absolutely can and will kill processes based on the process lifecycle.