The use of thread pools and the encapsulation of their tool classes

Source: Internet
Author: User
Tags exception handling
/** * 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}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.