Android AsyncTask 线程永不消亡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3077461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:36:06  来源:igfitidea点击:

AsyncTask threads never die

androidmultithreadingandroid-asynctask

提问by Computerish

I'm using AsyncTasks to fetch data in response to the user pressing a button. This works well and keeps the interface responsive while fetching the data, but when I checked out what was going on in the Eclipse debugger, I found out that every time a new AsyncTaskwas created (which is quite often, because they can only be used once), a new thread was being created but never terminated.

我正在使用AsyncTasks 来获取数据以响应用户按下按钮。这很有效并且在获取数据时保持界面响应,但是当我检查 Eclipse 调试器中发生的事情时,我发现每次AsyncTask创建一个新的(这很常见,因为它们只能使用一次),正在创建一个新线程,但从未终止。

The result is a large number of AsyncTaskthreads just sitting there. I'm not sure if this is a problem in practice or not, but I'd really like to get rid of those extra threads.

结果是大量AsyncTask线程只是坐在那里。我不确定这在实践中是否存在问题,但我真的很想摆脱那些额外的线程。

How can I kill these threads?

我怎样才能杀死这些线程?

回答by CommonsWare

AsyncTaskmanages a thread pool, created with ThreadPoolExecutor. It will have from 5 to 128 threads. If there are more than 5 threads, those extra threads will stick around for at most 10 seconds before being removed. (note: these figures are for the presently-visible open source code and vary by Android release).

AsyncTask管理一个线程池,用ThreadPoolExecutor. 它将有 5 到 128 个线程。如果线程数超过 5 个,则这些多余的线程最多会停留 10 秒钟,然后再被移除。(注意:这些数字适用于目前可见的开源代码,因 Android 版本而异)。

Leave the AsyncTaskthreads alone, please.

请不要管这些AsyncTask线程。

回答by Tom de Waard

In addition to CommonsWare's response:

除了 CommonsWare 的回应:

Currently I'm using Android 2.2, and my application uses no more than one AsyncTask at any time, but I'm creating a new one every x minutes. At first new AsyncTask Threads start to appear (a new Thread for a new AsyncTask) but after 5 threads (as mentioned by CommonsWare) they just stay visible in the debug window, and get re-used when new AsyncTask threads are needed. They just stay there until the debugger disconnects.

目前我使用的是 Android 2.2,我的应用程序在任何时候使用的 AsyncTask 都不超过一个,但我每 x 分钟创建一个新的。起初,新的 AsyncTask 线程开始出现(一个新的 AsyncTask 的新线程),但在 5 个线程之后(如 CommonsWare 所述),它们只是在调试窗口中保持可见,并在需要新的 AsyncTask 线程时重新使用。他们只是呆在那里,直到调试器断开连接。

回答by karnbo

Same symptom here. In my case the threads hanged around after I'd killed the Activity, and I was hoping for the App to close completely. Problem partly solved by using a single threaded executor:

这里的症状相同。在我的情况下,线程在我杀死 Activity 后挂起,我希望应用程序完全关闭。通过使用单线程执行器部分解决了问题:

    myActiveTask.executeOnExecutor(Executors.newSingleThreadExecutor());

This made the thread to vanish after the completing its work.

这使得线程在完成其工作后消失。

回答by Ravindra babu

Use ThreadPoolExecutor.

使用ThreadPoolExecutor

BlockingQueue workQueue= new LinkedBlockingQueue<Runnable>(100); // Work pool size
ThreadPoolExecutor executor = new ThreadPoolExecutor(
            Runtime.getRuntime().availableProcessors(),       // Initial pool size
            Runtime.getRuntime().availableProcessors(),       // Max pool size
            1, // KEEP_ALIVE_TIME
            TimeUnit.SECONDS, //  KEEP_ALIVE_TIME_UNIT
            workQueue);

Post your Runnabletask by using execute()method.

Runnable使用execute()方法发布您的任务 。

void execute (Runnable command)

Executes the given task sometime in the future. The task may execute in a new thread or in an existing pooled thread

在未来的某个时间执行给定的任务。任务可以在新线程或现有池线程中执行

Regarding your second query:

关于您的第二个查询:

How can I kill these threads?

我怎样才能杀死这些线程?

Once you are done with all your work with ThreadPoolExecutor, shutdown it properly as quoted in below post:

完成所有工作后ThreadPoolExecutor,请按照以下帖子中的引用正确关闭它:

How to properly shutdown java ExecutorService

如何正确关闭java ExecutorService