简体   繁体   English

如何在android应用程序中停止所有工作线程

[英]How to stop all worker threads in android application

how to stop all running worker threads in an android application without stoping the main thread? 如何在不停止主线程的情况下停止Android应用程序中所有正在运行的工作线程?

Any example for this? 这有什么例子吗?

Actually thread has method stop() , so you can go over all worker threads and call this method on each one. 实际上线程有方法stop() ,所以你可以遍历所有工作线程并在每个线程上调用此方法。

The question is "where to obtain list of worker threads?" 问题是“在哪里获取工作线程列表?” Better solution is to store this list somewhere at application level, ie every time you are creating worker thread put it to special list. 更好的解决方案是将此列表存储在应用程序级别的某个位置,即每次创建工作线程时都将其放入特殊列表。 This is better solution because you and only you know that the thread is "worker" thread. 这是更好的解决方案,因为您和只有您知道该线程是“工作”线程。

But theoretically you can even discover your application dynamically and retrieve the threads. 但理论上,您甚至可以动态发现应用程序并检索线程。 There is static method Thread.enumerate(Thread[] threads) that fills provided array. 有静态方法Thread.enumerate(Thread[] threads)填充提供的数组。 But how to know how many threads are running now? 但是如何知道现在有多少线程在运行? Use Thread.activeCount() . 使用Thread.activeCount()

Thread[] threads = new Thread[Thread.activeCount()];
Thread.enumerate(threads);
for (Thread t : threads) {
    if (isWorkerThread(t)) {
        t.stop();
    }
}

It is up to you to identify your worker threads. 由您来识别您的工作线程。 For example you can use the thread's name or the thread's stack trace for this. 例如,您可以使用线程的名称或线程的堆栈跟踪。

BUT it is a crime to call deprecated method stop() . 但是调用弃用方法stop()是犯罪行为。 Please refer to javadoc of this method to read about the reasons. 请参考此方法的javadoc来了解原因。

The "right" method is to implement graceful shutdown mechanism at application layer. “正确”的方法是在应用层实现优雅的关闭机制。 Each thread should check some flag that says whether thread should shutdown and when flag is true just return from run() method. 每个线程都应该检查一些标志,说明线程是否应该关闭,当flag为true时,只需从run()方法返回。 In this case it is very simple to close your working threads: just set value of this flag to true and threads will terminate themselves. 在这种情况下,关闭工作线程非常简单:只需将此标志的值设置为true,线程将自行终止。 This is the "right" solution. 这是“正确”的解决方案。

this post talks a ton about threads, please read and repost if that does not answer your question 这篇文章谈论了很多关于线程的内容,请阅读并重新发布,如果这不能回答你的问题

Stopping/Destroying a Thread 停止/销毁线程

in java, dont supply the method to stop thread. 在java中,不提供停止线程的方法。

you can only interrupt thread, but the thread must in the state which can be interrupt, like sleep , wait , etc... 你只能interrupt线程,但是线程必须处于可以中断的状态,比如sleepwait ......

or you can using some tricks to make the thread throw exception , such as: 或者您可以使用一些技巧使线程抛出异常 ,例如:

1.if the thread is connect the network, you want to stop thread, you can close the network connection, will throw the ioexception; 1.如果线程是连接网络,你要停止线程,你可以关闭网络连接,将抛出ioexception;

2.if the thread is read the file, you can close the stream to throw ioexception; 2.如果线程被读取文件,则可以关闭流以抛出ioexception;

3.if the thread is query the database, you can close the database 3.如果线程正在查询数据库,则可以关闭数据库

so it depend on your thread working. 所以它取决于你的线程工作。

There are two ways to do it. 两种方法可以做到这一点。

Make the threads interrupted 使线程中断

 Thread.interrupt()

Does it solve the problem? 它解决了这个问题吗? No it doesn't.The thread will only be interrupted only if they are in blocking/waiting state. 不,它不会。线程只有在它们处于阻塞/等待状态时才会被中断。 Calling thread.interrupted doesn't stop a thread. 调用thread.interrupted不会停止线程。 Then how does it help? 那怎么回事? The code that you are trying to Run. 您尝试运行的代码。 Make a check in operations like long running network operations,DB operations.This way you can interrupt most of the threads. 检查长时间运行的网络操作,数据库操作等操作。这样就可以中断大多数线程。

Kill the process and restart it 终止进程并重新启动它

   Kill the app process and restart it from zygote (It might not be for all devs)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM