springboot其中最重要的一个功能是自动配置,启动的时候基本就是单线程的,当然不是绝对的,读源码的时候发现了其中一个地方使用到了多线程,这里记录一下.
springboot自动配置有几个重要的注解
在自己写start的jar包时,都是在spring.facroties中写自己的配置类,在配置类中用@Bean向容器中注册bean,并经常的配合下面几个注解联合使用,因为spirng的自动配置功能是最后解析的,所以要配合按条件的方式注入spring容器,避免自己代码和自动配置类注入相同的Bean
ConditionalOnBean
ConditionalOnClass
ConditionalOnMissingBean
ConditionalOnMissingClass
上图解释了springboot自动配置类是在最后解析的,可以关注@EnableAutoConfiguration注解的@Import引入的类,这里不过多介绍了
图中的ConditionalOnClass有引用到了OnClassCondition类
org.springframework.boot.autoconfigure.condition.OnClassCondition#getOutcomes
@Override
protected final ConditionOutcome[] getOutcomes(String[] autoConfigurationClasses,
AutoConfigurationMetadata autoConfigurationMetadata) {
// Split the work and perform half in a background thread if more than one
// processor is available. Using a single additional thread seems to offer the
// best performance. More threads make things worse.
if (autoConfigurationClasses.length > 1 && Runtime.getRuntime().availableProcessors() > 1) {
//其中的一个条件判断系统的核心数
return resolveOutcomesThreaded(autoConfigurationClasses, autoConfigurationMetadata);
}
else {
OutcomesResolver outcomesResolver = new StandardOutcomesResolver(autoConfigurationClasses, 0,
autoConfigurationClasses.length, autoConfigurationMetadata, getBeanClassLoader());
return outcomesResolver.resolveOutcomes();
}
}
执行resolveOutcomesThreaded方法的其中一个条件是系统大于一个核心数,因为如果只有一个核心,使用多线程时,线程的切换反而会增加代码执行时间
private ConditionOutcome[] resolveOutcomesThreaded(String[] autoConfigurationClasses,
AutoConfigurationMetadata autoConfigurationMetadata) {
int split = autoConfigurationClasses.length / 2;
//新得线程,执行这行代码得同时新线程开始运行
OutcomesResolver firstHalfResolver = createOutcomesResolver(autoConfigurationClasses, 0, split,
autoConfigurationMetadata);
//当前spring运行得主线程
OutcomesResolver secondHalfResolver = new StandardOutcomesResolver(autoConfigurationClasses, split,
autoConfigurationClasses.length, autoConfigurationMetadata, getBeanClassLoader());
//主线程运行,注意新线程已经在上一行代码就开始运行了
ConditionOutcome[] secondHalf = secondHalfResolver.resolveOutcomes();
//让新线程join,保证新线程执行结束
ConditionOutcome[] firstHalf = firstHalfResolver.resolveOutcomes();
ConditionOutcome[] outcomes = new ConditionOutcome[autoConfigurationClasses.length];
System.arraycopy(firstHalf, 0, outcomes, 0, firstHalf.length);
System.arraycopy(secondHalf, 0, outcomes, split, secondHalf.length);
return outcomes;
}
上段代码得createOutcomesResolver方法和firstHalfResolver.resolveOutcomes方法,大家可以点进去看一下,很容易看到线程的创建,运行和join.
在平时的代码中我们也可以借鉴spring的这种方式,当然只限于项目启动的时候,比如在bean的后置处理器或者@PostConstruct标注的方法等,在运行的时候还是要使用线程池的.