OS 如何确定要挂起哪些线程?

How does the OS determine which threads to suspend?

标题不好,请阅读此说明以了解我的意思。

我创建了一个我想要 运行 很长时间的后台服务,根据定义 android 上的服务可以 运行 即使用户切换应用程序甚至关闭它。

我的问题是:当应用程序进入后台时,android 管理系统如何知道要保留哪些线程以及要擦除哪些线程,而唯一 运行 唯一的事情是服务?

我的服务 运行 默认在主线程上,所以当我想执行一个长任务时,我会这样做: AsyncTask.THREAD_POOL_EXECUTOR.execute(runnable);

是吗?我可以使用这个由 android 创建的默认线程池,还是服务必须显式创建线程以便系统知道即使应用程序进入后台线程也必须存活?

how does android management system knows which threads to keep and which to wipe when the app goes background and the only running thing is the service?

Android 没有 "wipe" 任何线程。 Android 终止 进程 以释放系统 RAM,而不是线程。

is that right?

我建议使用您自己的 Executor、RxJava 或 Kotlin 协程,而不是使用 AsyncTask 中的执行程序。那是因为我预计 AsyncTask 会在某个时候被弃用,因为它是 "old school".

但是,除此之外,您所做的听起来还不错。