initializeBean方法:
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
// 省略。。。。
// 对于实现BeanNameAware接口的类,将执行setBeanName方法,
// 我觉的setBeanName的作用更倾向于日志方面的记录,比如Spring async线程池
// taskScheduler名称
// 的日志打印记录,就是用了这个方法,获取name信息
invokeAwareMethods(beanName, bean);
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
// @PostConstruct执行时机,如果想再项目启动后执行某些操作,
//使用 CommandLineRunner或ApplicationRunner
// 这俩方法差不多,都是在refresh执行后执行callRunners进行调用的
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
//调用初始化方法invokeInitMethods,在invokeInitMethods中调用实现了
//InitializingBean的afterPropertiesSet方法。
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
TaskExecutor:函数式接口实现了Executor
SchedulingTaskExecutor:接口默认default方法,执行短任务优先,目前未发现用处
CustomizableThreadCreator:负责线程创建,默认用户线程daemon=false
ExecutorConfigurationSupport:线程池配置和生命周期管理
销毁方法:
public void shutdown() {
if (logger.isInfoEnabled()) {
logger.info("Shutting down ExecutorService" + (this.beanName != null ? " '" + this.beanName + "'" : ""));
}
if (this.executor != null) {
if (this.waitForTasksToCompleteOnShutdown) {
this.executor.shutdown();
}
else {
for (Runnable remainingTask : this.executor.shutdownNow()) {
cancelRemainingTask(remainingTask);
}
}
awaitTerminationIfNecessary(this.executor);
}
}
private void awaitTerminationIfNecessary(ExecutorService executor) {
if (this.awaitTerminationSeconds > 0) {
try {
if (!executor.awaitTermination(this.awaitTerminationSeconds, TimeUnit.SECONDS)) {
if (logger.isWarnEnabled()) {
logger.warn("Timed out while waiting for executor" +
(this.beanName != null ? " '" + this.beanName + "'" : "") + " to terminate");
}
}
}
catch (InterruptedException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Interrupted while waiting for executor" +
(this.beanName != null ? " '" + this.beanName + "'" : "") + " to terminate");
}
Thread.currentThread().interrupt();
}
}
}
对于flume的线程生命周期停止的过程
public void stop() {
LOGGER.info("Configuration provider stopping");
executorService.shutdown();
try {
if (!executorService.awaitTermination(500, TimeUnit.MILLISECONDS)) {
LOGGER.debug("File watcher has not terminated. Forcing shutdown of executor.");
executorService.shutdownNow();
while (!executorService.awaitTermination(500, TimeUnit.MILLISECONDS)) {
LOGGER.debug("Waiting for file watcher to terminate");
}
}
} catch (InterruptedException e) {
LOGGER.debug("Interrupted while waiting for file watcher to terminate");
Thread.currentThread().interrupt();
}
lifecycleState = LifecycleState.STOP;
LOGGER.debug("Configuration provider stopped");
}
与netty、flume相比,spring线程池的生命周期管理,觉得更加面向对象,里边的设计还是很漂亮的
参考:
关闭线程池 shutdown 和 shutdownNow 的区别
http://www.matools.com/api/java8