/** * thread Pool Management class */public class Mythreadpoolmanager {/** * Description: The following constants I am based on the asynctask source configuration, you can configure according to their own needs */
Based on the number of CPUs dynamically configure the number of core threads and the maximum number of threads private static final int cpu_count = Runtime.getruntime (). Availableprocessors ();
Number of core threads = Number of CPU cores + 1 private static final int core_pool_size = Cpu_count + 1;
Thread pool Max threads = number of CPU cores * 2 + 1 private static final int maximum_pool_size = Cpu_count * 2 + 1;
Non-core thread timed out when idle 1s private static final int keep_alive = 1; To ensure that the class has only one instance object, avoid generating too many object consumption resources, use singleton mode private Mythreadpoolmanager () {} private static Mythreadpoolmanager s
Instance; Public synchronized static Mythreadpoolmanager Getsinstance () {if (sinstance = = null) {sinstance = n
EW Mythreadpoolmanager ();
} return sinstance;
}//thread pool object private Threadpoolexecutor executor; Using the thread pool, threads are created entirely by the line pool itself, and we do not need to create any threads//all we need to do is throw one task to another in this pool public void ExecuTe (Runnable R) {if (executor = = null) {/** * corepoolsize: Number of core threads * Maximump
Oolsize: Maximum number of threads that the thread pool accommodates (Workqueue queue is full) * KeepAliveTime: Non-core thread idle time Timeout * unit:keepalivetime units
* WorkQueue: Waiting queue, storing tasks not yet performed * Threadfactory: Thread-Created Factory * Handler: Exception handling mechanism * */executor = new Threadpoolexecutor (core_pool_size, Maximum_pool_size, Keep_al IVE, Timeunit.seconds, New arrayblockingqueue<runnable>, Executors.defaultthreadfactory (), New
Threadpoolexecutor.abortpolicy ());
} executor.execute (R);//throw a task into the thread pool} public void Cancel (Runnable r) {if (r! = null) { Executor.getqueue (). Remove (R);//Removes the task from the Waiting queue}}}