简体   繁体   中英

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?

Any example for this?

Actually thread has method stop() , so you can go over all worker threads and call this method on each one.

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. But how to know how many threads are running now? Use 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() . Please refer to javadoc of this method to read about the reasons.

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. 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. 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.

you can only interrupt thread, but the thread must in the state which can be interrupt, like sleep , wait , etc...

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;

2.if the thread is read the file, you can close the stream to throw ioexception;

3.if the thread is query the database, you can close the database

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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