基於隊列的線程池

基於隊列的線程池java

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestThreadPool {
    //    public static BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(10);
    public static ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(10);

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            queue.add(new TestThread("初始化"));
        }

        final ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 3, 15, TimeUnit.SECONDS, queue);
        System.out.println("getActiveCount=" + executor.getActiveCount()
                        + ";getKeepAliveTime=" + executor.getKeepAliveTime(TimeUnit.SECONDS)
                        + ";getCompletedTaskCount=" + executor.getCompletedTaskCount()
                        + ";getCorePoolSize=" + executor.getCorePoolSize()
                        + ";getLargestPoolSize=" + executor.getLargestPoolSize()
                        + ";getMaximumPoolSize=" + executor.getMaximumPoolSize()
                        + ";getPoolSize=" + executor.getPoolSize()
                        + ";getTaskCount=" + executor.getTaskCount()
                        + ";getQueue().size()=" + executor.getQueue().size()
        );
        executor.execute(queue.poll());
        System.out.println("getActiveCount=" + executor.getActiveCount()
                        + ";getKeepAliveTime=" + executor.getKeepAliveTime(TimeUnit.SECONDS)
                        + ";getCompletedTaskCount=" + executor.getCompletedTaskCount()
                        + ";getCorePoolSize=" + executor.getCorePoolSize()
                        + ";getLargestPoolSize=" + executor.getLargestPoolSize()
                        + ";getMaximumPoolSize=" + executor.getMaximumPoolSize()
                        + ";getPoolSize=" + executor.getPoolSize()
                        + ";getTaskCount=" + executor.getTaskCount()
                        + ";getQueue().size()=" + executor.getQueue().size()
        );

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("getActiveCount=" + executor.getActiveCount()
                                    + ";getKeepAliveTime=" + executor.getKeepAliveTime(TimeUnit.SECONDS)
                                    + ";getCompletedTaskCount=" + executor.getCompletedTaskCount()
                                    + ";getCorePoolSize=" + executor.getCorePoolSize()
                                    + ";getLargestPoolSize=" + executor.getLargestPoolSize()
                                    + ";getMaximumPoolSize=" + executor.getMaximumPoolSize()
                                    + ";getPoolSize=" + executor.getPoolSize()
                                    + ";getTaskCount=" + executor.getTaskCount()
                                    + ";getQueue().size()=" + executor.getQueue().size()
                    );
                    try {
                        Thread.currentThread().sleep(1000L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (true) {
                    queue.add(new TestThread("生產者"));
                    try {
                        Thread.currentThread().sleep(500L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    i++;
                    if (i > 10) break;
                }
            }
        }).start();
    }
}

class TestThread implements Runnable {
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private String name;        //建立者
    private Date addDate;       //添加到隊列的日期

    TestThread(String name) {
        this.name = name;
        this.addDate = new Date();
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() +
                ":建立者=" + name + ",建立時間=" + sdf.format(addDate) + ",執行時間=" + sdf.format(new Date()) + ",當前隊列大小=" + TestThreadPool.queue.size());
        try {
            Thread.currentThread().sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


執行結果:
ide

getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=0;getCorePoolSize=2;getLargestPoolSize=0;getMaximumPoolSize=3;getPoolSize=0;getTaskCount=3;getQueue().size()=3
getActiveCount=1;getKeepAliveTime=15;getCompletedTaskCount=0;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=3;getQueue().size()=2
pool-1-thread-1:建立者=初始化,建立時間=2014-08-20 12:08:19,執行時間=2014-08-20 12:08:19,當前隊列大小=2
getActiveCount=1;getKeepAliveTime=15;getCompletedTaskCount=0;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=3;getQueue().size()=2
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=1;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=6;getQueue().size()=4
pool-1-thread-1:建立者=初始化,建立時間=2014-08-20 12:08:19,執行時間=2014-08-20 12:08:20,當前隊列大小=4
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=2;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=8;getQueue().size()=5
pool-1-thread-1:建立者=初始化,建立時間=2014-08-20 12:08:19,執行時間=2014-08-20 12:08:21,當前隊列大小=5
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=3;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=10;getQueue().size()=6
pool-1-thread-1:建立者=生產者,建立時間=2014-08-20 12:08:19,執行時間=2014-08-20 12:08:22,當前隊列大小=6
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=4;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=12;getQueue().size()=7
pool-1-thread-1:建立者=生產者,建立時間=2014-08-20 12:08:19,執行時間=2014-08-20 12:08:23,當前隊列大小=7
getActiveCount=1;getKeepAliveTime=15;getCompletedTaskCount=5;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=8
pool-1-thread-1:建立者=生產者,建立時間=2014-08-20 12:08:20,執行時間=2014-08-20 12:08:24,當前隊列大小=8
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=6;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=7
pool-1-thread-1:建立者=生產者,建立時間=2014-08-20 12:08:20,執行時間=2014-08-20 12:08:25,當前隊列大小=7
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=7;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=6
pool-1-thread-1:建立者=生產者,建立時間=2014-08-20 12:08:21,執行時間=2014-08-20 12:08:26,當前隊列大小=6
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=8;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=5
pool-1-thread-1:建立者=生產者,建立時間=2014-08-20 12:08:21,執行時間=2014-08-20 12:08:27,當前隊列大小=5
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=9;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=4
pool-1-thread-1:建立者=生產者,建立時間=2014-08-20 12:08:22,執行時間=2014-08-20 12:08:28,當前隊列大小=4
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=10;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=3
pool-1-thread-1:建立者=生產者,建立時間=2014-08-20 12:08:22,執行時間=2014-08-20 12:08:29,當前隊列大小=3
getActiveCount=1;getKeepAliveTime=15;getCompletedTaskCount=11;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=2
pool-1-thread-1:建立者=生產者,建立時間=2014-08-20 12:08:23,執行時間=2014-08-20 12:08:30,當前隊列大小=2
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=12;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=1
pool-1-thread-1:建立者=生產者,建立時間=2014-08-20 12:08:23,執行時間=2014-08-20 12:08:31,當前隊列大小=1
getActiveCount=1;getKeepAliveTime=15;getCompletedTaskCount=12;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=1
pool-1-thread-1:建立者=生產者,建立時間=2014-08-20 12:08:24,執行時間=2014-08-20 12:08:32,當前隊列大小=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0
getActiveCount=0;getKeepAliveTime=15;getCompletedTaskCount=14;getCorePoolSize=2;getLargestPoolSize=1;getMaximumPoolSize=3;getPoolSize=1;getTaskCount=14;getQueue().size()=0

Process finished with exit code -1


這個是理想狀況,若是生產者建立速度大於消費者速度,則會隨着時間推移耗盡系統資源,這個須要經過RejectedExecutionHandler來實現。this


對這個例子,作了一些改動,能夠更加清楚看到執行過程:線程

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestThreadPool {
    //    public static BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(10);
//    public static ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(10);
//    public static ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(10);
    public static LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();

    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            queue.add(new TestThread("初始化"));
        }

        final ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 3, 15, TimeUnit.SECONDS, queue);
//        System.out.println("getActiveCount=" + executor.getActiveCount()
//                        + ";getKeepAliveTime=" + executor.getKeepAliveTime(TimeUnit.SECONDS)
//                        + ";getCompletedTaskCount=" + executor.getCompletedTaskCount()
//                        + ";getCorePoolSize=" + executor.getCorePoolSize()
//                        + ";getLargestPoolSize=" + executor.getLargestPoolSize()
//                        + ";getMaximumPoolSize=" + executor.getMaximumPoolSize()
//                        + ";getPoolSize=" + executor.getPoolSize()
//                        + ";getTaskCount=" + executor.getTaskCount()
//                        + ";getQueue().size()=" + executor.getQueue().size()
//        );
//        executor.execute(new Thread());
        executor.prestartCoreThread();
//        System.out.println("getActiveCount=" + executor.getActiveCount()
//                        + ";getKeepAliveTime=" + executor.getKeepAliveTime(TimeUnit.SECONDS)
//                        + ";getCompletedTaskCount=" + executor.getCompletedTaskCount()
//                        + ";getCorePoolSize=" + executor.getCorePoolSize()
//                        + ";getLargestPoolSize=" + executor.getLargestPoolSize()
//                        + ";getMaximumPoolSize=" + executor.getMaximumPoolSize()
//                        + ";getPoolSize=" + executor.getPoolSize()
//                        + ";getTaskCount=" + executor.getTaskCount()
//                        + ";getQueue().size()=" + executor.getQueue().size()
//        );

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println("getActiveCount=" + executor.getActiveCount()
                                    + ";getKeepAliveTime=" + executor.getKeepAliveTime(TimeUnit.SECONDS)
                                    + ";getCompletedTaskCount=" + executor.getCompletedTaskCount()
                                    + ";getCorePoolSize=" + executor.getCorePoolSize()
                                    + ";getLargestPoolSize=" + executor.getLargestPoolSize()
                                    + ";getMaximumPoolSize=" + executor.getMaximumPoolSize()
                                    + ";getPoolSize=" + executor.getPoolSize()
                                    + ";getTaskCount=" + executor.getTaskCount()
                                    + ";getQueue().size()=" + executor.getQueue().size()
                    );
                    try {
                        Thread.currentThread().sleep(200L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (true) {
                    queue.add(new TestThread("生產者"));
                    try {
                        Thread.currentThread().sleep(100L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    i++;
                    if (i > 100) break;
                }
            }
        }).start();
    }
}

class TestThread implements Runnable {
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private String name;        //建立者
    private Date addDate;       //添加到隊列的日期

    TestThread(String name) {
        this.name = name;
        this.addDate = new Date();
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() +
                ":建立者=" + name + ",建立時間=" + sdf.format(addDate) + ",執行時間=" + sdf.format(new Date()) + ",當前隊列大小=" + TestThreadPool.queue.size());

        System.out.println(TestThreadPool.queue.peek());
        try {
            Thread.currentThread().sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
相關文章
相關標籤/搜索