如果我无限期在后台运行Android线程会发生什么 [英] What happens if I run an Android thread in the background indefinitely

查看:449
本文介绍了如果我无限期在后台运行Android线程会发生什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android文档表明Oreo对后台执行有新的限制: https://developer .android.com/about/versions/oreo/background .这似乎是合理的,并且他们明确地打算使自己的平台更像iOS,并防止应用程序在后台猖ramp运行.

Android docs indicate that Oreo has new restrictions on background execution: https://developer.android.com/about/versions/oreo/background. This seems reasonable and they're clearly aiming to make their platform more like iOS and prevent apps running rampant in the background.

我不清楚的一件事(实际上根本没有记录)是当UI进入后台时您可以在线程上执行的操作.具体来说,

The thing that's unclear to me (in fact, not documented at all) is what can you do on a thread when the UI goes to the background. Specifically,

鉴于我创建了一个线程

GIVEN I create a thread with

new Thread(() -> { 
     // Naughty thread doing something forever
}).start();

然后我将应用发送到后台

AND I send the app to the background

那时...该线程发生了什么事?

THEN ...what happens to that thread?

我已经创建了非常简单的代码来执行此操作,并且我的线程已经愉快地将logcat切换了10分钟以上而没有任何问题.

I've created very simple code to do this and my thread has been happily churning out to logcat for 10+ minutes with no issues.

有人对此线程有什么限制有任何清楚的信息吗?我本以为,由于Android限制了后台服务可以执行的操作,因此它也将限制此类线程可以执行的操作.

Does anyone have any clear information on what restrictions there are on such threads? I would have thought that since Android restricts what a background service can do that it would also restrict what such threads can do.

请注意,我们没有计划编写一款可以执行此类操作的应用程序.我们只希望能够编写安全的代码,而不会在较新版本的android上引起问题.在iOS上,如果您进入后台,那么您将有一段宽限期来结束您所做的任何事情(并且您可以要求更多的时间),但是最终您的线程将被挂起.

Note that we have no plans to write an app which does anything like this. We just want to be able to write safe code which doesn't cause issues on newer versions of android. On iOS, if you go to the background then you get a period of grace to finish off whatever you're doing (and you can ask for more time) but eventually your thread will be suspended.

推荐答案

任何人都没有关于存在哪些限制的任何明确信息 在这样的线程上?我本来以为,由于Android的限制 后台服务可以做什么,这也将限制 线程可以做到.

Does anyone have any clear information on what restrictions there are on such threads? I would have thought that since Android restricts what a background service can do that it would also restrict what such threads can do.

此类线程可以运行多长时间没有任何限制.只要您的应用程序在后台运行,您就可以继续在后台执行Thread

There are no restrictions as such on how long such threads can run. As long as your app is running in background, you can continue to execute Thread in background

但是,由于线程不会无休止地运行,因此您需要考虑如何优雅地终止/释放线程.如果操作系统在内存不足时需要放弃内存,则托管此后台活动的应用程序进程将终止,最终破坏线程.如果处理不当,将导致线程/内存泄漏.

However, you need to consider how to gracefully terminate/release the Thread, since the Thread won't run endlessly. If OS needs to relinquish memory during memory crunch then the process of your app hosting this background Activity will be terminated, eventually destroying the thread. If not handled properly, this would lead to Thread/memory leaks.

这篇关于如果我无限期在后台运行Android线程会发生什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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