AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
for(inti=0;i<numberOfTasks;i++){inttaskId=i+1;inttaskDuration=1000;// 1000 msTestTasktask=newTestTask(taskId,taskDuration);// こう書くと、非同期実行になる// ソース(sdk v.21)を見ると、並列実行ができるExecutorで処理されるぽいtask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);}// ========================// 以下はAsyncTask.THREAD_POOL_EXECUTORの正体。// androidのソースから抜いた// ========================privatestaticfinalintCPU_COUNT=Runtime.getRuntime().availableProcessors();privatestaticfinalintCORE_POOL_SIZE=CPU_COUNT+1;privatestaticfinalintMAXIMUM_POOL_SIZE=CPU_COUNT*2+1;privatestaticfinalintKEEP_ALIVE=1;privatestaticfinalThreadFactorysThreadFactory=newThreadFactory(){privatefinalAtomicIntegermCount=newAtomicInteger(1);publicThreadnewThread(Runnabler){returnnewThread(r,"AsyncTask #"+mCount.getAndIncrement());}};privatestaticfinalBlockingQueue<Runnable>sPoolWorkQueue=newLinkedBlockingQueue<Runnable>(128);/** * An {@link Executor} that can be used to execute tasks in parallel. */publicstaticfinalExecutorTHREAD_POOL_EXECUTOR=newThreadPoolExecutor(CORE_POOL_SIZE,MAXIMUM_POOL_SIZE,KEEP_ALIVE,TimeUnit.SECONDS,sPoolWorkQueue,sThreadFactory);