Java中使用线程池与不使用线程池的比较

/**
 * 测试使用线程池和未使用线程池的执行效果
 */
package com.test;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 *
 */
public class TestThreadPool {

    /**
     * 使用线程池
     */
    public void UseThreadPool(int count) {

        long startTime = System.currentTimeMillis();

        final List<Integer> l = new LinkedList<Integer>();
        /**
         * corePoolSize - 池中所保存的线程数,包括空闲线程。
         * maximumPoolSize - 池中允许的最大线程数。
         * keepAliveTime - 当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。
         * unit - keepAliveTime 参数的时间单位。
         * workQueue - 执行前用于保持任务的队列。此队列仅保持由 execute 方法提交的 Runnable 任务。
         *
         */
        
        ThreadPoolExecutor tp = new ThreadPoolExecutor(1, 1, 60,
                TimeUnit.SECONDS, new LinkedBlockingQueue<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.print("測試使用線程池:");
        System.out.println(System.currentTimeMillis() - startTime);

    }

    /**
     * 未使用線程池
     *
     * @param count
     * 產生隨機數的個數
     */
    public void unUseThreadPool(int count) {

        //當前時間
        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() {
                //執行Run方法
                public void run() {
                    l.add(random.nextInt());
                }
            };
               //開始線程
            thread.start();

            try {
                //線程加入
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

        System.out.print("測試未使用線程池:");
        System.out.println(System.currentTimeMillis() - startTime);
        System.out.println("集合的大小:" + l.size());

    }
    
    
    public static void main(String[] args) {
        
        TestThreadPool testpool = new TestThreadPool();
        
        testpool.UseThreadPool(200000);
        testpool.unUseThreadPool(200000);
        
    }

}

 

------------------------------------------------------------------------------------------测试结果------------------------------------------------------------

測試使用線程池:155
測試未使用線程池:16806
集合的大小:200000

 

当COUNT值变大后,主观上明显感觉执行速度大相径庭。。。。

posted @ 2016-03-13 10:38  千古风流人物  阅读(549)  评论(0编辑  收藏  举报