要的android停止线程最好的和安全的方式 [英] android best and safe way to stop thread

查看:136
本文介绍了要的android停止线程最好的和安全的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是停在Android的线程的最佳途径。我知道我可以使用,而不是它的AsyncTask,并且有取消()方法。我必须使用线程在我的处境。下面是我如何使用线程:

I want to know which is the best way to stop a thread in Android. I know I can use AsyncTask instead of it and that there is cancel() method. I have to use Threads in my situation. Here is how I'm using thread :

Runnable runnable = new Runnable() {
        @Override
        public void run() {
            //doing some work
        }
    };
new Thread(runnable).start();

因此​​,任何人有任何想法,这是停止线程的最佳方法是什么?

So anyone have any idea which is the best way to stop a thread?

推荐答案

您应该让您的线程支持中断。基本上,你可以叫 yourThread.interrupt()停止线程,并在你的run()你需要定期检查 Thread.interrupted()

You should make your thread support interrupts. Basically, you can call yourThread.interrupt() to stop the thread and, in your run() method you'd need to periodically check the status of Thread.interrupted()

这是一个很好的教程这里

这篇关于要的android停止线程最好的和安全的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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