如何停止android中的ASyncTask线程

发布于 2024-10-08 00:31:51 字数 169 浏览 0 评论 0原文

can anybody have any idea how to stop ASyncTask thread in android?.

实际上我有一个循环来创建线程并执行它们。当这个循环结束时,我想停止所有已运行的线程。 有什么办法可以停止线程吗?

多谢。

can anybody have any idea how to stop ASyncTask thread in android?.

Actually i have a loop which creates threads and executes them. and when this loop will end i want to stop all that threads which have run.
is there anyway to stop threads?

thanks alot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不离久伴 2024-10-15 00:31:51

根据我使用 AsyncTask 的经验,调用 cancel(boolean mayInterruptIfRunning) 并不一定会停止后台进程的执行。似乎发生的事情是 AsyncTask 将执行 onCancelled(),并且不会运行 onPostExecute() > 当它完成时。该行为可能完全取决于 doInBackgound() 中的代码

In my experience with AsyncTask, calling cancel(boolean mayInterruptIfRunning) doesn't necessarily stop the execution of the background process. All that seems to happen is that the AsyncTask will execute onCancelled(), and won't run onPostExecute() when it completes. The behaviour probably depends on exactly what code you have in doInBackgound()

恍梦境° 2024-10-15 00:31:51

就我而言,我使用 HttpPost、HttpGet 等执行服务器请求。我发现结束 AsyncTask 的唯一方法是调用 abort() 方法,如以下示例所示

if (isCancelled) {
     try {
         //cancel the task and immediately abort the HttpRequest
         uploadTask.cancel(true);
         post.abort();
     } catch (UnsupportedOperationException e) {
         e.printStackTrace();
     }
} 

In my case I perform server requests with HttpPost, HttpGet and so on. The only way I found to end an AsyncTask was calling the abort() method as you can see in the following example

if (isCancelled) {
     try {
         //cancel the task and immediately abort the HttpRequest
         uploadTask.cancel(true);
         post.abort();
     } catch (UnsupportedOperationException e) {
         e.printStackTrace();
     }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文