首页  

java线程池实例     所属分类 java 浏览量 1333
线程池创建 核心参数 同步队列 拒绝策略 
线程池关闭


int coreSize = 1;
int maxSize = 3;
BlockingQueue<Runnable> workQueue = new SynchronousQueue<Runnable>();
ThreadFactory threadFactory = Executors.defaultThreadFactory();
RejectedExecutionHandler rejectHandler = new AbortPolicy();
ThreadPoolExecutor pool = new ThreadPoolExecutor(coreSize, maxSize, 60, TimeUnit.SECONDS,workQueue,threadFactory,rejectHandler);
		
		
        int num = 9;
		for (int i = 0; i < num; i++) {
			try {
				Task task = new Task(1000);
				pool.execute(task);
			} catch (Exception e) {
				System.out.println(e);
			}
		}
		


使用同步队列 作为 任务队列 ,该队列容量为 0
使用默认的拒绝策略 AbortPolicy
调用 execute 方法提交任务
使用同步队列容量为0 , 工作线程数达到 maxSize , 再提交任务时 执行拒绝策略




shutdown 和 shutdownNow 的区别 

shutdown  提交的任务会被正常执行
shutdownNow 中段正在执行的任务,并返回任务队列中的任务
Task 中的 sleep 方法会中段 返回 

doSleep error,java.lang.InterruptedException: sleep interrupted

使用 同步队列 , shutdownNow 返回 任务列表 为 空 

使用 ArrayBlockingQueue 
workQueue = new ArrayBlockingQueue(2);
shutdownNow 返回 任务列表 大小为 2




完整代码 ThreadPoolTest.java


import java.util.Date; import java.util.List; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Executors; import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor.AbortPolicy; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; public class ThreadPoolTest { public static void main(String[] args) throws Exception { int coreSize = 1; int maxSize = 3; BlockingQueue<Runnable> workQueue = new SynchronousQueue<Runnable>(); // workQueue = new ArrayBlockingQueue<Runnable>(2); ThreadFactory threadFactory = Executors.defaultThreadFactory(); RejectedExecutionHandler rejectHandler = new AbortPolicy(); ThreadPoolExecutor pool = new ThreadPoolExecutor(coreSize, maxSize, 60, TimeUnit.SECONDS,workQueue,threadFactory,rejectHandler); int num = 9; for (int i = 0; i < num; i++) { try { Task task = new Task(1000); pool.execute(task); } catch (Exception e) { System.out.println(e); } } // pool.shutdown(); List<Runnable> list = null; list = pool.shutdownNow(); System.out.println("list.size=" + list.size()); System.out.println("shutdown done,"+new Date()); doSleep(3000); } private static class Task implements Runnable { private static final AtomicLong seq = new AtomicLong(0); private long sleep; private String name; public Task(long sleep) { this.sleep = sleep; this.name = "task-" + seq.getAndIncrement(); } public void run() { System.out.println(this.name + " start,sleep=" + sleep + "," + new Date()); doSleep(sleep); System.out.println(this.name + " done,sleep=" + sleep + "," + new Date()); } } private static void doSleep(long time){ if(time<=0){ return; } try{ Thread.sleep(time); }catch(Throwable e){ // 保留中断标志 // Thread.currentThread().interrupt(); System.out.println("doSleep error,"+e); } } }

上一篇     下一篇
员工离职前的9种征兆

ThreadPoolExecutor中的ctl变量

2019年一季度A股投资者结构

tomcat线程池要点

java线程池系列文章汇总

tomcat参数 acceptCount maxConnections maxThreads