package com.thinker.thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolTest{

	static class MyThread implements Runnable{

		private String name;

		public MyThread(String name){
			this.name = name;
		}

		@Override
		public void run(){
			try{
				Thread.sleep(1000);
				System.out.println(name + " finished job!");
			}catch(InterruptedException e){
				e.printStackTrace();
			}
		}
	}


	public static void main(String[] args){
		BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
		ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4, 5, TimeUnit.SECONDS, queue, new ThreadPoolExecutor.AbortPolicy());
		for (int i=0; i< 10; i++){
			System.out.println("当前线程池大小[" + threadPool.getPoolSize() + "],当前队列大小[" + queue.size() + "]");
			threadPool.execute(new MyThread("Thread" + i));
		}
		threadPool.shutdown();
	}

}
