Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.concurrent.Executor; import android.annotation.SuppressLint; import android.os.AsyncTask; import android.os.Build; public class Main { public static <Params, Progress, Result> void executeParallel(AsyncTask<Params, Progress, Result> task, Params... args) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, args); } else { task.execute(args); } } /** * In earlier versions of android, AsyncTasks where run on different threads * by employing a {@link ThreadPoolExecutor}. However, in more recent * versions, they all use only one single thread. If this behavior is not * desired, a new method * {@link AsyncTask#executeOnExecutor(Executor, Object...)} was introduced. * However, this method is only available since honeycomb. This helper * method mitigates this change in the framework. For older versions it just * ignores the executor parameter. * * @param task * An {@link AsyncTask} * @param executor * The executor which should host the task. * @param args * Optional arguments to be passed to the task. */ @SuppressLint("NewApi") public static <Params, Progress, Result> void executeOnExecutor(AsyncTask<Params, Progress, Result> task, Executor executor, Params... args) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { task.executeOnExecutor(executor, args); } else { task.execute(args); } } }