baekacaek
baekacaek

Reputation: 1557

How to use ExecutorService shutdown in Android application

Im writing an SDK that has a singleton class with ExecutorService. It looks something like this:

public class MySingleton {
    private static MySingleton mInstance;
    private ExecutorService mExecutorService;

    private MySingleton() {
        mExecutorService = Executors.newSingleThreadExecutor();
    }

    // ...

    public void doSomething(Runnable runnable) {
        mExecutorService.execute(runnable);
    }
}

This SDK class is intended to be used throughout the application to run tasks/Runnables, and doSomething() function is to queue and run all Runnables in a single thread.

But one thing I couldn't figure out is when to call the ExecutorService.shutdown() method. If I call it like so:

public void doSomething(Runnable runnable) {
    if (mExecutorService.isTerminated()) {
        mExecutorService = Executors.newSingleThreadExecutor();
    }
    mExecutorService.execute(runnable);
    mExecutorService.shutdown();
}

It would defeat the purpose of using one Thread because if the old Runnable is still running when doSomething() is called the 2nd time, there may be two different Threads running simultaneously. Of course I can have a function that manually shuts down the ExecutorService, but requiring the user of the SDK to explicitly call the shutdown function didn't seem appropriate.

Can anyone show me some tips on when/how to call ExecutorService.shutdown() in an Android application? Thanks

Upvotes: 1

Views: 3329

Answers (2)

Ananta Raha
Ananta Raha

Reputation: 1071

In an Android application, there is no need to shutdown a singleton ExecutorService unless it has any idle thread. According to Android docs:

A pool that is no longer referenced in a program AND has no remaining threads will be shutdown automatically. If you would like to ensure that unreferenced pools are reclaimed even if users forget to call shutdown(), then you must arrange that unused threads eventually die, by setting appropriate keep-alive times, using a lower bound of zero core threads and/or setting allowCoreThreadTimeOut(boolean).

So if you use Executors.newCachedThreadPool() or create a ThreadPoolExecutor with corePoolSize of 0, it will automatically be shutdown when the application process dies.

Upvotes: 1

marcinj
marcinj

Reputation: 49986

There is no good reason to call shutdown each time you execute some task. You might want to call shutdown when some part of your application is being closed/finished. Ie. when Service is being stopped - then if it used executors - then I suppose you should shutdown them - but actually the point is to allow all the tasks to finish before the service quit logic will perform some finishing code. ie. by using:

  executors.shutdown();
  if (!executors.awaitTermination(5, TimeUnit.SECONDS)) {
    executors.shutdownNow();
  }

as an example, such service could be used to download some files, user would ie. want to pause downloading - ie. by opening camera application (that might stop your application/service to reclaim its resources/memory).

Upvotes: 1

Related Questions