Compare threads with thread pools and instances ., Thread Pool instance comparison

Source: Internet
Author: User

Compare threads with thread pools and instances ., Thread Pool instance comparison

Thread Pool:

int count = 200000;        long startTime = System.currentTimeMillis();        final List<Integer> l = new LinkedList<Integer>();        ThreadPoolExecutor tp = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(count));        final Random random = new Random();        for (int i = 0; i < count; i++) {            tp.execute(new Runnable() {                                @Override                public void run() {                    l.add(random.nextInt());                }            });        }        tp.shutdown();        try {            tp.awaitTermination(1, TimeUnit.DAYS);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(System.currentTimeMillis() - startTime);        System.out.println(l.size());

Output result:

1 1722 200000

Thread:

int count = 200000;        long startTime = System.currentTimeMillis();        final List<Integer> l = new LinkedList<Integer>();        final Random random = new Random();        for (int i = 0; i < count; i++) {            Thread thread = new Thread(){                @Override                public void run(){                    l.add(random.nextInt());                }            };            thread.start();            try {                thread.join();            } catch (InterruptedException e) {                e.printStackTrace();            }        }        System.out.println(System.currentTimeMillis() - startTime);        System.out.println(l.size());

Output result:

1 335562 200000

Conclusion: The difference is that the thread pool is used to reuse threads, instead of using the thread pool, the thread is created every time. The execution in the thread is very simple, and the overhead of the thread creation accounts for a large proportion of the whole time.

Copy the Translation results to Google

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.