标签:html 操作 ast handle hand 线程调度 for 在线 初始化
参考博客1:http://www.cnblogs.com/exe19/p/5359885.html
参考博客2:http://www.jianshu.com/p/87bff5cc8d8c
1 . 线程池的体系结构
java.util.concurrent.Executor [I]是一个顶层接口,在它里面只声明了一个方法execute(Runnable),返回值为void,参数为Runnable类型, |-- ExecutorService [I]继承了Executor,线程池的主要接口 |-- AbstractExecutorService [A]抽象类,实现了ExecutorService中的大部分方法 |-- ThreadPoolExecutor [C]线程池的主要实现类 |-- ScheduledExecutorService [I]继承了ExecutorService,负责线程调度的接口 |-- ScheduledExecutorService [C]继承 ThreadPoolExecutor,实现 ScheduledExecutorService
2 .线程池的使用
public static void method1() { Executor pool = Executors.newFixedThreadPool(5); for(int i=0;i<5;i++){ pool.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); } }); } } 1、Executors.newFixedThreadPool(5)初始化一个包含5个线程的线程池pool; 2、通过pool.execute方法提交1个任务,该任务打印当前的线程名; 3、负责执行任务的线程的生命周期都由Executor框架进行管理;
3 . ThreadPoolExecutor类
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { if (corePoolSize < 0 || maximumPoolSize <= 0 || maximumPoolSize < corePoolSize || keepAliveTime < 0) throw new IllegalArgumentException(); if (workQueue == null || threadFactory == null || handler == null) throw new NullPointerException(); this.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.workQueue = workQueue; this.keepAliveTime = unit.toNanos(keepAliveTime); this.threadFactory = threadFactory; this.handler = handler; }
TimeUnit.DAYS; //天
TimeUnit.HOURS; //小时
TimeUnit.MINUTES; //分钟
TimeUnit.SECONDS; //秒
TimeUnit.MILLISECONDS; //毫秒
TimeUnit.MICROSECONDS; //微妙
TimeUnit.NANOSECONDS; //纳秒
execute()方法
public void execute(Runnable command) { if (command == null) throw new NullPointerException(); /* * Proceed in 3 steps: * 此过程分为3步: * 1. If fewer than corePoolSize threads are running, try to * start a new thread with the given command as its first * task. The call to addWorker atomically checks runState and * workerCount, and so prevents false alarms that would add * threads when it shouldn‘t, by returning false. * 1.如果正在运行的线程数小于corePoolSize,则尝试启动一个新 * 的线程来执行指定的任务。对addWorker 方法的调用会原子性 * 的检查runState (运行状态)和workerCount(工作集数量 ), * 通过返回false阻止不应该出现的添加错误。 * * 2. If a task can be successfully queued, then we still need * to double-check whether we should have added a thread * (because existing ones died since last checking) or that * the pool shut down since entry into this method. So we * recheck state and if necessary roll back the enqueuing if * stopped, or start a new thread if there are none. * 2. 如果一个线程可以成功加入队列,无论我们已经将线 * 程添加进去了还是在进入这个方法时线程池已经关闭了都需 * 要继续做两次检查(因为上次检查之后可能存在一个死掉的 * 线程)所以就再次检查状态,并且如果必要就会滚加入队列的 * 操作或者在一个也没有的时候启动一个新的线程。 * * 3. If we cannot queue task, then we try to add a new * thread. If it fails, we know we are shut down or saturated * and so reject the task. * 如果任务无法加入队列,就尝试添加一个新的线程,如果失败, * 说明线程池关闭了或者队列已经满了所以拒绝了这次任务 */ int c = ctl.get(); if (workerCountOf(c) < corePoolSize) { if (addWorker(command, true)) return; c = ctl.get(); } if (isRunning(c) && workQueue.offer(command)) { int recheck = ctl.get(); if (! isRunning(recheck) && remove(command)) reject(command); else if (workerCountOf(recheck) == 0) addWorker(null, false); } else if (!addWorker(command, false)) reject(command); }
submit()方法
此方法是在ExecutorService中声明的方法,在AbstractExecutorService就已经有了具体的实现,在ThreadPoolExecutor中并没有对其进行重写,这个方法也是用来向线程池提交任务的,但是它和execute()方法不同,它能够返回任务执行的结果,去看submit()方法的实现,会发现它实际上还是调用的execute()方法,只不过它利用了Future来获取任务执行结果
shutdown()和shutdownNow()方法
这两个方法是用来关闭线程池的。
标签:html 操作 ast handle hand 线程调度 for 在线 初始化
原文地址:http://www.cnblogs.com/pepper7/p/7196932.html