简体   繁体   English

关闭应用程序后,守护进程线程仍然存在

[英]Daemon Thread is still alive after closing the app

I'm just testing with an almost empty android project . 我只是在测试一个几乎空的android项目

I created a daemon thread in the Main Activity. 我在主活动中创建了一个守护程序线程。

Started the app, and pressed the back button on the phone to close the app. 启动该应用程序,然后按手机上的后退按钮以关闭该应用程序。

But the daemon thread never dies. 但是守护进程线程永远不会死。

Daemon Thread works really well with an empty JAVA PROJECT . Daemon Thread与一个空的JAVA PROJECT一起工作得很好。

But with android, doesn't work. 但是使用android则无法正常工作。

I searched for the solution for hours but only got this. 我搜索了几个小时的解决方案,但只有这个。

A daemon thread only runs as long as there are non-daemon threads running. 守护程序线程仅在有非守护程序线程在运行时才运行。 When the last non-daemon thread ends, the runtime will exit. 当最后一个非守护程序线程结束时,运行时将退出。 This is not normally relevant to applications with a UI. 这通常与具有UI的应用程序无关。

Does anyone know how to solve this? 有谁知道如何解决这个问题?

PS. PS。 I stopped the app not by pressing the hardware back button but by pressing the 'Force stop' button in 'App info', and both of the app and the thread are gone! 我不是通过按硬件后退按钮而是通过按“应用程序信息”中的“强制停止”按钮来停止应用程序的,因此应用程序和线程都消失了!

I first learned about Daemon Thread from some example codes for android develoment. 我首先从android开发的一些示例代码中学到了Daemon Thread。

But nobody mentioned about what I've experienced. 但是没有人提及我的经历。

Do android developers just believe Daemon Threads will be killed for sure? android开发人员是否只相信Daemon Threads肯定会被杀死?

But the daemon thread never dies. 但是守护进程线程永远不会死。

Presumably, you did not cancel() the thread. 大概您没有cancel()线程。 You need to do this at an appropriate point. 您需要在适当的时候这样做。

But with android, doesn't work. 但是使用android则无法正常工作。

It works just fine. 它工作正常。 It works the same as in Java, as you can tell by reading your quoted passage. 通过阅读引用的段落可以看出它的作用与Java中的作用相同。 The difference is in how long your process lives and in the threads in that process (Android processes have other threads besides ones that you fork). 区别在于您的进程有多长时间以及该进程中的线程(Android进程除您派生的线程外还有其他线程)。

Does anyone know how to solve this? 有谁知道如何解决这个问题?

Call cancel() on your thread at an appropriate point. 在适当的时候在线程上调用cancel()

Pressing back button does not close the app (assuming that closing means process termination). 按下返回按钮不会关闭应用程序(假设关闭意味着进程终止)。 That only finishes an activity but process is still running. 这仅完成一项活动,但进程仍在运行。 To achieve desired behavior you can use eg. 为了达到期望的行为,您可以使用例如。 AsyncTask and call cancel() from appropriate callback of your Activity eg. AsyncTask并从您的Activity的适当回调中调用cancel() ,例如。 in onDestroy() or onBackPressed() if you are interested exactly in back button presses. 如果您确实对按下后退按钮感兴趣,请在onDestroy()onBackPressed()中。

apologies for pointing possible basic out but follow your problem have you ensured after closing that its not running in you Task Manager (ctrl+alt+delete)..have task manager running alongside your daemon then during the closing of daemon watch what happens during and after closing in task manager as it may be backing up files in accordance with settings-check settings in daemon....or have you used an ExecutorService to run as Daemon in Java in which case this will prevent your program from shutting down see here ExecutorService - 10 tips and tricks 道歉,指出可能的基本要领,但要解决您的问题,请确保在关闭后确保其未在任务管理器中运行(ctrl + alt + delete)。让任务管理器与守护程序一起运行,然后在守护程序关闭期间观察发生的情况和在任务管理器中关闭后,因为它可能正在根据daemon中的settings-check设置来备份文件...。或者您已使用ExecutorService在Java中以Daemon的身份运行,在这种情况下,这将防止您的程序关闭,请参见此处ExecutorService-10个提示和技巧

Java Applicatioin Process Hangs on Windows and Cached Thread Pool Java应用程序进程在Windows和高速缓存的线程池上挂起

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java线程中断和连接(连接后线程仍然存在) - Java thread interrupts and joins (thread is still alive after join) 关闭应用程序并且服务仍在运行后的NullPointerException - NullPointerException after Closing App with Service still running 关闭项目后线程仍然退出,如何将其杀死? - Thread still exits after closing project, how to kill it? 在 onDestroy 之后 Loader 还活着 - Loader is still alive after onDestroy 关闭应用程序后,LocationUpdatesForegroundService应用程序进程仍在运行 - LocationUpdatesForegroundService app process is still running after closing app 如果守护程序线程正在运行,如何使程序保持活动状态? - How to keep my program alive for as long a daemon thread is running? 在主线程退出后,恶魔线程继续运行,并且还有一些非守护程序线程还在运行? - Demon threads continue to run after main exited and there is some non-daemon thread still running? 关闭终端后使Jboss实例保持活动状态 - Keep Jboss instance alive after closing the terminal 如果我知道线程名称,如何检查tomcat中的线程是否仍然存在? - How to check if a thread is still alive in tomcat if I know the thread name? 会话无效后,它仍然存在,为什么? - After invalidating session , it's still alive , why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM