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.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
//赋值
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
Java 有 1 5 10
默认工厂是默认优先级 5
Daemon 是 false //不是守护线程,所以默认线程池创建出来的是用户线程
Java提供了两种线程:守护线程和用户线程
守护线程,是指在程序运行时 在后台提供一种通用服务的线程,
这种线程并不属于程序中不可或缺的部分。
守护线程,是指在程序运行时 在后台提供一种通用服务的线程,
这种线程并不属于程序中不可或缺的部分。
通俗点讲,任何一个守护线程都是整个JVM中所有非守护线程的"保姆"。
用户线程和守护线程几乎一样,唯一的不同之处在于如果用户线程已经全部退出运行,
只剩下守护线程存在了,JVM也就退出了。
因为当所有非守护线程结束时,没有了被守护者,守护线程也就没有工作可做,
当然也就没有继续执行的必要了,程序就会终止,同时会杀死所有的"守护线程",
也就是说只要有任何非守护线程还在运行,程序就不会终止
在Java语言中,守护线程一般具有较低的优先级,它并非只由JVM内部提供,
用户在编写程序时也可以自己设置守护线程,
例如:将一个线程设置为守护线程的方法就是在调用start()
启动线程之前调用对象的setDaemon(true)方法,若将以上参数设置为false,
则表示的是用户进程模式,
需要注意的是,
当在一个守护线程中产生了其他的线程,那么这些新产生的线程默认还是守护线程,
用户线程也是如此。
/**
* The default thread factory
*/
static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
默认拒绝策略:直接抛出 RejectExecutionException异常
public static class AbortPolicy implements RejectedExecutionHandler {
public AbortPolicy() { }
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
调用者自己执行
public static class CallerRunsPolicy implements RejectedExecutionHandler {
public CallerRunsPolicy() { }
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
r.run();
}
}
}
什么事都没干
public static class DiscardPolicy implements RejectedExecutionHandler {
public DiscardPolicy() { }
rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}
扔了队列中最早的任务,再把这个任务添加进来
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
public DiscardOldestPolicy() { }
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
e.getQueue().poll();
e.execute(r);
}
}
}
ok,鉴于源码阶段太耗时,开学水课再看,记录进度:下次该看
ThreadPoolExecutor源码一第11集 ThreadPoolExecutor