LoginSignup
0
1

More than 3 years have passed since last update.

AsyncTaskをTHREAD_POOL_EXECUTORで実行した際のPoolサイズ

Last updated at Posted at 2020-03-14

AsyncTaskをexecuteOnExecutor(THREAD_POOL_EXECUTOR)で実行した際のPoolサイズについて調べました。

結果

THREAD_POOL_EXECUTORのPoolサイズはCPUの数で決まり、PoolサイズはMath.max(2, Math.min(CPU_COUNT - 1, 4))となります。

詳細

THREAD_POOL_EXECUTORは、AsyncTaskで生成されたThreadPoolExecutorで、生成時のパラメータは下記の通りです。

CcorePoolSize:Math.max(2, Math.min(CPU_COUNT - 1, 4))
maximumPoolSize:CPU_COUNT * 2 + 1
※ workQueuのサイズは128

AsyncTask.java
    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    // We want at least 2 threads and at most 4 threads in the core pool,
    // preferring to have 1 less than the CPU count to avoid saturating
    // the CPU with background work
    private static final int CORE_POOL_SIZE = Math.max(2, Math.min(CPU_COUNT - 1, 4));
    private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
    private static final int KEEP_ALIVE_SECONDS = 30;

    private static final BlockingQueue<Runnable> sPoolWorkQueue =
            new LinkedBlockingQueue<Runnable>(128);

    public static final Executor THREAD_POOL_EXECUTOR;
    static {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
                sPoolWorkQueue, sThreadFactory);
        threadPoolExecutor.allowCoreThreadTimeOut(true);
        THREAD_POOL_EXECUTOR = threadPoolExecutor;
    }

ThreadPoolExecutorのmaximumPoolSize

ThreadPoolExecutorのPoolサイズは、corePoolSizeおよびmaximumPoolSizeによって設定された境界に従って、自動的に調整されます。
corePoolSizeを超えているが、maximumPoolSizeスレッドが実行されていない場合、キューがいっぱいの場合にのみ新しいスレッドが作成されます。
※ THREAD_POOL_EXECUTORのworkQueuのサイズは128

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1