当关机的ExecutorService的Andr​​oid应用程序 [英] When to shutdown executorservice in android application

查看:134
本文介绍了当关机的ExecutorService的Andr​​oid应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我打算在我的整个应用程序使用一个ExecutorService的,对此我送新的可运行的或可调用的执行/顺从,而我ommit关机之后我做到这一点。我只想把我的任务的ExecutorService的,让他处理它们,并执行他们给它的资源(他有多少线程有可用的,而且他有多少可以创建,如果需要,然后排队相应的任务)。

Lets assume I'm planning to use one executorservice in my entire application, to which I'm sending new runnable's or callable's to execute/submit, and I ommit to shutdown right after I do that. I just want to throw my "tasks" to the executorservice and let him handle them and execute them giving it's resources (how many threads he has available, and how many he can create if needed and then queue those tasks accordingly).

从你的Andr​​oid应用程序使用的ExecutorService,并考虑到应用程序状态变化的经验,如果我不希望不断关闭和做这个重新创建的ExecutorService:

From your experience of using ExecutorService in Android application, and taking into account the application state changes, if I don't want to constantly shutdown and re-create the executorservice by doing this:

    executor = Executors.newCachedThreadPool();
    executor.submit(some Runnable);
    executor.shutdown();

,什么时间,你会在哪里reccomend关机服务,然后恢复它,这样我可以prevent一些泄漏或一些不可预见的后果?

, what time and where would you reccomend to shutdown service, and then reinstate it so that I can prevent some leaks or some unforseen consequences?

我主要是来的定做:

1)关程序通过后退按钮上的backstack的最后一项活动(应用程序使用的许多活动) 2)应用程序会在后台(在任何这些活动) 3)申请去回到前台(在任何这些活动的)

1) Closing app by back button on the last activity in the backstack (Application uses many activities) 2) Application going in the background (on any of those activities) 3) Application going back into foreground (on any of those activities)

推荐答案

您可以做各地的一部作品。创建执行人作为守护进程。然后,当你的应用程序退出时将自动结束。你不必显式调用关闭

You can do one work around. Create the executor as daemon. Then it will automatically end when your application exits. You don't have to explicitly call the shutdown.

 ExecutorService es = Executors.newSingleThreadExecutor( new ThreadFactory() {
    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r);
        t.setDaemon(true);
        return t;
    }
});

这篇关于当关机的ExecutorService的Andr​​oid应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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