RejectedPolicy--DiscardPolicy

package com.jerry.concurrency;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestRejectedPolicy {

	public static void main(String[] args) throws InterruptedException {
		ThreadPoolExecutor pool = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, 
				new ArrayBlockingQueue<Runnable>(1));//设置线程池只启动一个线程 阻塞队列一个元素
		pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
		//设置策略为直接丢弃
		for (int i = 0; i < 10; i++) {
			pool.submit(new Runnable() {
				public void run() {
					System.out.println("线程:" + Thread.currentThread().getName()+"  开始执行");
					try {
						Thread.sleep(1000L);
					} catch (Exception e) {
						e.printStackTrace();
					}
					System.out.println("线程:" + Thread.currentThread().getName()+"  执行完毕");
				}
			});
		}
		Thread.sleep(9000L);
		pool.shutdown();
		System.out.println("关闭后线程终止了吗?" + pool.isTerminated());
	}

}

你可能感兴趣的:(policy)