java-Thread Pool-Tool class __JDK

Source: Internet
Author: User

Summarize the use of the four common thread pools that the JDK takes with you

Package thread.pool;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.ScheduledExecutorService;

Import Java.util.concurrent.TimeUnit;

    public class Threadpoolutil {static Executorservice cachedthreadpool;

    Static Executorservice Fixedthreadpool;

    Static Scheduledexecutorservice Scheduledthreadpool;

    Static Executorservice Singlethreadpool; Static Executorservice Cachedthreadpoolrun (Runnable Runnable) {if (Cachedthreadpool = null) cachedth
        Readpool = Executors.newcachedthreadpool ();
        Cachedthreadpool.submit (runnable);
    return cachedthreadpool;
            public static Executorservice Fixedthreadpoolrun (Runnable Runnable) {if (Fixedthreadpool = null)
        Fixedthreadpool = Executors.newfixedthreadpool (Runtime.getruntime (). Availableprocessors ());
        Fixedthreadpool.submit (runnable);
    return fixedthreadpool; } pUblic static Executorservice Scheduledthreadpoolrun (Runnable Runnable, long delay, timeunit unit) {if (Scheduledt
                    Hreadpool = = null) Scheduledthreadpool = Executors.newscheduledthreadpool (Runtime.getruntime ()
        . Availableprocessors ());
        Scheduledthreadpool.schedule (runnable, delay, unit);
    return scheduledthreadpool;
            public static Executorservice Singlethreadpoolrun (Runnable Runnable) {if (Singlethreadpool = null)
        Singlethreadpool = Executors.newsinglethreadexecutor ();
        Singlethreadpool.submit (runnable);
    return singlethreadpool;
        public static void Main (string[] args) {final Long starttime = System.currenttimemillis ();

        Executorservice executorservice = null;
            for (int i = 0; i < 1000000 i++) {//System.out.println (Finali);
            final int finali = i;
            Runnable task = ()-> System.out.println (Finali); ExecUtorservice = Threadpoolutil.fixedthreadpoolrun (Task);
        } if (Executorservice!= null) {Executorservice.shutdown (); } while (true) {if (executorservice.isterminated ()) {System.out.println ("All child threads are ended" 。
                ");
            Break
        } final Long stoptime = System.currenttimemillis ();
        System.out.println ("---------------------------");


    System.out.println (Stoptime-starttime); }

}
Related Article

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.