我们有什么钩子可以在应用程序退出时终止工作线程

发布于 2024-10-31 14:09:07 字数 1289 浏览 1 评论 0原文

我正在从应用程序的“主”线程生成新的工作线程。只要应用程序处于活动状态,这些线程就不断需要执行某些操作。而且,当应用程序退出时,我确实希望正确清理所有工作线程(无论这意味着什么)。为了做到这一点,生命周期方法是唯一的地方吗?应用程序退出时是否会收到意图/通知?如果是,我们可以在此类意图/通知上注册广播接收器来执行所需的清理吗?

谢谢,

阿肖克。

Code Snippet:

导入 android.app.Activity; 导入 android.os.Bundle; 导入 android.util.Log;

公共类 SomeThreadTest 扩展 Activity { /** 首次创建活动时调用。 */ 线程 t = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    t = new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(15000);
                    Log.d("SomeThreadTest", "Thread wokeup now");
                } catch (InterruptedException e) {
                    Log.d("SomeThreadTest", "Thread interrupted exception called!");
                    e.printStackTrace();
                    return;
                }

                if (Thread.interrupted()) {
                    Log.d("SomeThreadTest", "I'm interrupted!");
                }
            }
        }
    }, "TestThread");

    t.setDaemon(true);
    t.start();
}

}

I'm spawning new worker threads from the 'main' thread of the application. These threads are constantly required to do some operation, as long as the application is alive. And, when the application exits, I do want all the worker threads to be cleaned up (whatever that means) properly. In order to do this, are life cycle methods the only place? Are there are intents/notifications which are received when an application exits ? If yes, can we register a broadcast receiver on such intents/notifications to do the required cleanup?

Thanks,

Ashok.

Code Snippet:

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class SomeThreadTest extends Activity {
/** Called when the activity is first created. */
Thread t = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    t = new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(15000);
                    Log.d("SomeThreadTest", "Thread wokeup now");
                } catch (InterruptedException e) {
                    Log.d("SomeThreadTest", "Thread interrupted exception called!");
                    e.printStackTrace();
                    return;
                }

                if (Thread.interrupted()) {
                    Log.d("SomeThreadTest", "I'm interrupted!");
                }
            }
        }
    }, "TestThread");

    t.setDaemon(true);
    t.start();
}

}

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

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

发布评论

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

评论(2

英雄似剑 2024-11-07 14:09:07

考虑调用workerThread.setDaemon(true),它告诉线程是服务提供者,并在非守护程序主线程退出时杀死我。

Consider calling workerThread.setDaemon(true) which tells the thread that is is a service provider and to KILL ME if the non daemon main thread quits.

有木有妳兜一样 2024-11-07 14:09:07

Android 具有守护线程和关闭挂钩,这可能会对您有所帮助,因为应用程序在其自己的虚拟机中执行。

守护线程会在虚拟机退出时退出,并且不会阻止虚拟机退出。创建线程后,调用 thread.setDaemon(true)< /a> 将线程标记为守护线程。因此,如果您标记所有工作线程守护线程,VM 将在退出时处理这些线程。

或者,当虚拟机退出时,可以使用关闭钩子自行清理线程。要注册关闭挂钩,请调用 运行时。 addShutdownHook(Thread),传递一个尚未启动的Thread。

Android has daemon threads and shutdown hooks, which may help you here, since the application executes in it's own VM.

A daemon thread exits when the vm exits, and doesn't stop the vm from exiting. After creating a thread, call thread.setDaemon(true) to mark the thread as a daemon thread. So, if you mark all your worker threads daemon threads, the VM will dispose of these threads when it exits.

Alternatively, a shutdown hook can be used to clean up the threads yourself when the vm exits. To register a shutdown hook, call Runtime.addShutdownHook(Thread), passing a Thread that has not been started yet.

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