ThreadPoolExecutor的使用

来源:互联网 发布:做淘宝新手怎么找货源 编辑:程序博客网 时间:2024/06/08 06:13

//下面任务的执行顺序:

//0,1马上执行(2个预先在的thread被使用)

//2,3放入任务栈(sPoolWorkQueue.size为2)

//4:马上执行(还可以创建一个thread用来执行任务,3-2=1)

//5:被拒绝,因为既不能马上被执行,又不能放入Queue


public class TestThread2 {    private static final BlockingQueue<Runnable> sPoolWorkQueue =            new LinkedBlockingQueue<Runnable>(2);    public static void main(String[] args) {        //2:表示线程池中可供使用的线程数目        //3:表示线程池中最大的线程数目        //sPoolWorkQueue的size:可Queue的任务数目        Executor executor = new ThreadPoolExecutor(2, 3, 1, TimeUnit.SECONDS, sPoolWorkQueue);        //Executor executor1 = Executors.newSingleThreadExecutor();        for(int i = 0; i < 10; i++){            System.out.println("input begin i: " + i);            final int index = i;            executor.execute(new Runnable() {                                @Override                public void run() {                    String name = Thread.currentThread().getName();                    System.out.println("run begin: " + index + ", name = " + name);                    try {                        Thread.currentThread().sleep(30000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println("run end: " + index + ", name = " + name);                }            });            System.out.println("input end i: " + i);        }    }}


0 0
原创粉丝点击