如何停止在Android的线程和处理程序 [英] How to stop threads and handlers in Android

查看:150
本文介绍了如何停止在Android的线程和处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

老实说,我想不出它 - 我听说使用Thread.stop()是不使用一件好事。这还不算为我工作。我如何获得线程/处理程序停止运行!

I honestly can't figure it out - I've heard that thread.stop() is not a good thing to use. It also isn't working for me. How do I get threads/handlers to stop running!!

推荐答案

主题应以礼貌的方式终止。你应该建立一些机制,为你的线程停止。你可以有一个对您的线程的每个循环中检查挥发性布尔参数(假设你在那里循环),像这样:

Threads should be terminated in a "polite" way. You should build in some mechanism for your thread to stop. You can have a volatile boolean parameter that is checked on every loop of your thread (assuming you have loops in there) like so:

while (!threadStop) {
    // Do stuff
}

然后你就可以从另一个线程设置布尔值设置为false(请确保您处理,尽管所有的同步问题),你的线程将停止在它的下一次迭代。

And then you can set the boolean value to false from another thread (make sure you handle all synchronization issues though) and your thread will stop in it's next iteration.

这篇关于如何停止在Android的线程和处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆