Android 终止通过 runnable 创建的线程

发布于 2024-11-06 11:10:54 字数 574 浏览 5 评论 0原文

假设在我的应用程序中的各个点上,我创建并触发了一个新的可运行程序,如下所示:

new Thread(new Runnable() { 
    public void run() {
    while(true) {
       //do lots of stuff
       //draw lots of stuff on screen, have a good ol time
       //total loop processing time abt 1250-1500ms
       //check for conditions to stop the loop, break;
   }    }   }

现在,除了 while 循环中的 break; 之外,还有什么方法可以在执行中途终止该线程吗?我希望能够从父线程中专门立即终止它,就像用户刚刚请求加载不同的地图一样。每 5 行左右的代码后插入一个 if (stopFlag) break; (在父线程中设置)感觉很笨拙。

我查看了 Runnable 和 Thread 的方法,但没有看到它。有人知道一个很棒的技巧吗?

Let's say in various points in my application, I create and fire off a new runnable like so:

new Thread(new Runnable() { 
    public void run() {
    while(true) {
       //do lots of stuff
       //draw lots of stuff on screen, have a good ol time
       //total loop processing time abt 1250-1500ms
       //check for conditions to stop the loop, break;
   }    }   }

Now, is there any way to terminate that thread midway through execution other than break; inside my while loop? I'd like to be able to kill it specifically and immediately from the parent thread, like, as in the event that the user just requested to load a different map. It feels clunky to insert an if (stopFlag) break; (set in parent thread) after every 5 or so lines of code.

I peeked at Runnable's and Thread's methods and I just can't see it. Someone know an awesome trick?

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

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

发布评论

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

评论(3

冬天旳寂寞 2024-11-13 11:10:54

您可以使用 AsyncTask 并调用 cancel 取消线程。

You may use AsyncTask and call cancel to cancel the thread.

栀梦 2024-11-13 11:10:54

您可以检查当线程/可运行对象停止时将正确更改的条件或标志,而不是 while (true)。这似乎是自 Thread 以来的建议策略 .stop() 已被弃用。

Instead of while (true) you may check for a condition or a flag that would be changed properly when the Thread/Runnable should be stopped. This seems to be the suggested strategy since Thread.stop() has been deprecated.

太阳哥哥 2024-11-13 11:10:54

您可以按照建议使用 AsyncTask,这在这种情况下可能效果最好。我相信你也可以使用interrupt()方法,如果你不在Android中,这是首选方法,但仍然需要显式检查它是否被中断:

Thread t = new Thread(new Runnable() {
    public void run() {
        while (true) {
            // do some stuff
            if (isInterrupted()) {
                break;
            }
        }
     });
t.start();

// Whoa! Need to stop that work!
t.interrupt();

You could use AsyncTask as suggested, which probably works best in this case. I believe you can also use the interrupt() method, which is preferred if good if you're not in Android, but still suffers from having to explicitly check if it is interrupted:

Thread t = new Thread(new Runnable() {
    public void run() {
        while (true) {
            // do some stuff
            if (isInterrupted()) {
                break;
            }
        }
     });
t.start();

// Whoa! Need to stop that work!
t.interrupt();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文