geekymv

Do one thing, do it well.


  • 首页

  • 分类

  • 归档

  • 标签

  • 关于

spring-annotation

发表于 2021-07-14
1
2
3
4
5
6
7
8
9
10
11
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringAnnotationApplication {

public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);

MyBean bean = ctx.getBean(MyBean.class);
System.out.println(bean.getName());
}
}

AppConfig 配置类

1
2
3
4
5
6
7
8
9
10
11
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

@Bean
public MyBean myBean() {
return new MyBean("tom");
}
}

MyBean类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class MyBean {

private String name;

public MyBean() {
System.out.println("my bean");
}
public MyBean(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

AnnotationConfigApplicationContext 类的构造方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* Create a new AnnotationConfigApplicationContext, deriving bean definitions
* from the given annotated classes and automatically refreshing the context.
* @param annotatedClasses one or more annotated classes,
* e.g. {@link Configuration @Configuration} classes
*/
public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
// 调用默认构造方法
this();
// 注册注解配置类
register(annotatedClasses);
// 刷新
refresh();
}

/**
* Create a new AnnotationConfigApplicationContext that needs to be populated
* through {@link #register} calls and then manually {@linkplain #refresh refreshed}.
*/
public AnnotationConfigApplicationContext() {
this.reader = new AnnotatedBeanDefinitionReader(this);
this.scanner = new ClassPathBeanDefinitionScanner(this);
}

AnnotatedBeanDefinitionReader 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Create a new {@code AnnotatedBeanDefinitionReader} for the given registry.
* If the registry is {@link EnvironmentCapable}, e.g. is an {@code ApplicationContext},
* the {@link Environment} will be inherited, otherwise a new
* {@link StandardEnvironment} will be created and used.
* @param registry the {@code BeanFactory} to load bean definitions into,
* in the form of a {@code BeanDefinitionRegistry}
* @see #AnnotatedBeanDefinitionReader(BeanDefinitionRegistry, Environment)
* @see #setEnvironment(Environment)
*/
public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry) {
this(registry, getOrCreateEnvironment(registry));
}

/**
* Create a new {@code AnnotatedBeanDefinitionReader} for the given registry and using
* the given {@link Environment}.
* @param registry the {@code BeanFactory} to load bean definitions into,
* in the form of a {@code BeanDefinitionRegistry}
* @param environment the {@code Environment} to use when evaluating bean definition
* profiles.
* @since 3.1
*/
public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
Assert.notNull(environment, "Environment must not be null");
this.registry = registry;
this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);
// 注册Processor
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
}

AnnotationConfigUtils.registerAnnotationConfigProcessors 方法将注解相关的post processor 注册到registry

  • ConfigurationClassPostProcessor
  • AutowiredAnnotationBeanPostProcessor
  • RequiredAnnotationBeanPostProcessor
  • CommonAnnotationBeanPostProcessor
  • PersistenceAnnotationBeanPostProcessor
  • EventListenerMethodProcessor
  • DefaultEventListenerFactory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Register all relevant annotation post processors in the given registry.
* @param registry the registry to operate on
*/
public static void registerAnnotationConfigProcessors(BeanDefinitionRegistry registry) {
registerAnnotationConfigProcessors(registry, null);
}

/**
* Register all relevant annotation post processors in the given registry.
* @param registry the registry to operate on
* @param source the configuration source element (already extracted)
* that this registration was triggered from. May be {@code null}.
* @return a Set of BeanDefinitionHolders, containing all bean definitions
* that have actually been registered by this call
*/
public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
BeanDefinitionRegistry registry, Object source) {

DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
if (beanFactory != null) {
if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
}
if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
}
}

Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<BeanDefinitionHolder>(8);

if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
}

if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
}

if (!registry.containsBeanDefinition(REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(RequiredAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
}

// Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
}

// Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition();
try {
def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
AnnotationConfigUtils.class.getClassLoader()));
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
}
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
}

if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
}

if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
}

return beanDefs;
}

ClassPathBeanDefinitionScanner 类的构造方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Create a new {@code ClassPathBeanDefinitionScanner} for the given bean factory.
* @param registry the {@code BeanFactory} to load bean definitions into, in the form
* of a {@code BeanDefinitionRegistry}
*/
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry) {
this(registry, true);
}

public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
this(registry, useDefaultFilters, getOrCreateEnvironment(registry));
}

public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
Environment environment) {

this(registry, useDefaultFilters, environment,
(registry instanceof ResourceLoader ? (ResourceLoader) registry : null));
}


public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
Environment environment, ResourceLoader resourceLoader) {

Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
this.registry = registry;

if (useDefaultFilters) {
// 注册默认过滤器
registerDefaultFilters();
}
setEnvironment(environment);
setResourceLoader(resourceLoader);
}

ClassPathBeanDefinitionScanner 继承自 ClassPathScanningCandidateComponentProvider

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Register the default filter for {@link Component @Component}.
* <p>This will implicitly register all annotations that have the
* {@link Component @Component} meta-annotation including the
* {@link Repository @Repository}, {@link Service @Service}, and
* {@link Controller @Controller} stereotype annotations.
* <p>Also supports Java EE 6's {@link javax.annotation.ManagedBean} and
* JSR-330's {@link javax.inject.Named} annotations, if available.
*
*/
@SuppressWarnings("unchecked")
protected void registerDefaultFilters() {
this.includeFilters.add(new AnnotationTypeFilter(Component.class));
ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
try {
this.includeFilters.add(new AnnotationTypeFilter(
((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
logger.debug("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
}
catch (ClassNotFoundException ex) {
// JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
}
try {
this.includeFilters.add(new AnnotationTypeFilter(
((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));
logger.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
}
catch (ClassNotFoundException ex) {
// JSR-330 API not available - simply skip.
}
}

AnnotationConfigApplicationContext 类 register方法

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* Register one or more annotated classes to be processed.
* <p>Note that {@link #refresh()} must be called in order for the context
* to fully process the new classes.
* @param annotatedClasses one or more annotated classes,
* e.g. {@link Configuration @Configuration} classes
* @see #scan(String...)
* @see #refresh()
*/
public void register(Class<?>... annotatedClasses) {
Assert.notEmpty(annotatedClasses, "At least one annotated class must be specified");
this.reader.register(annotatedClasses);
}

调用 AnnotatedBeanDefinitionReader 类的 register 方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Register one or more annotated classes to be processed.
* <p>Calls to {@code register} are idempotent; adding the same
* annotated class more than once has no additional effect.
* @param annotatedClasses one or more annotated classes,
* e.g. {@link Configuration @Configuration} classes
*/
public void register(Class<?>... annotatedClasses) {
for (Class<?> annotatedClass : annotatedClasses) {
registerBean(annotatedClass);
}
}

/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations.
* @param annotatedClass the class of the bean
*/
@SuppressWarnings("unchecked")
public void registerBean(Class<?> annotatedClass) {
registerBean(annotatedClass, null, (Class<? extends Annotation>[]) null);
}


/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations.
* @param annotatedClass the class of the bean
* @param name an explicit name for the bean
* @param qualifiers specific qualifier annotations to consider,
* in addition to qualifiers at the bean class level
*/
@SuppressWarnings("unchecked")
public void registerBean(Class<?> annotatedClass, String name, Class<? extends Annotation>... qualifiers) {
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
return;
}

ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
abd.setScope(scopeMetadata.getScopeName());
String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
if (qualifiers != null) {
for (Class<? extends Annotation> qualifier : qualifiers) {
if (Primary.class == qualifier) {
abd.setPrimary(true);
}
else if (Lazy.class == qualifier) {
abd.setLazyInit(true);
}
else {
abd.addQualifier(new AutowireCandidateQualifier(qualifier));
}
}
}

BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
}

BeanDefinitionReaderUtils 工具类的registerBeanDefinition 方法将bean注册到registry。

@Bean注解
ConfigurationClassPostProcessor 类

ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod

springboot

发表于 2021-07-09

看看SpringBoot是如何加载META-INF/spring.factories 配置的

SpringApplication.run(BackupCloudWebApplication.class, args);

SpringFactoriesLoader 类
从多个classpath下的jar包中加载META-INF/spring.factories 配置,
META-INF/spring.factories 中的key是接口或抽象类,value是实现类,多个实现类使用逗号分割。
比如spring-beans-5.2.2.RELEASE.jar 中的spring.factories,内容如下:
org.springframework.beans.BeanInfoFactory=org.springframework.beans.ExtendedBeanInfoFactory

SpringApplication 类

1
2
3
4
5
6
7
8
9
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
this.webApplicationType = WebApplicationType.deduceFromClasspath();
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}

getSpringFactoriesInstances 方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private <T> Collection<T> getSpringFactoriesInstances(Class<T> type) {
return getSpringFactoriesInstances(type, new Class<?>[] {});
}

private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
ClassLoader classLoader = getClassLoader();
// 加载配置
// Use names and ensure unique to protect against duplicates
Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));

// 创建实例
List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
// 排序
AnnotationAwareOrderComparator.sort(instances);
return instances;
}

SpringFactoriesLoader 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public final class SpringFactoriesLoader {

/**
* The location to look for factories.
* <p>Can be present in multiple JAR files.
*/
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";


private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);

private static final Map<ClassLoader, MultiValueMap<String, String>> cache = new ConcurrentReferenceHashMap<>();


private SpringFactoriesLoader() {
}

// 省略其他方法...
}

SpringFactoriesLoader.loadFactoryNames 方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Load the fully qualified class names of factory implementations of the
* given type from {@value #FACTORIES_RESOURCE_LOCATION}, using the given
* class loader.
* @param factoryType the interface or abstract class representing the factory
* @param classLoader the ClassLoader to use for loading resources; can be
* {@code null} to use the default
* @throws IllegalArgumentException if an error occurs while loading factory names
* @see #loadFactories
*/
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
String factoryTypeName = factoryType.getName();
return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
}

private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
// 从缓存中取值
MultiValueMap<String, String> result = cache.get(classLoader);
if (result != null) {
return result;
}

try {
Enumeration<URL> urls = (classLoader != null ?
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
result = new LinkedMultiValueMap<>();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
UrlResource resource = new UrlResource(url);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
for (Map.Entry<?, ?> entry : properties.entrySet()) {
String factoryTypeName = ((String) entry.getKey()).trim();
for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
result.add(factoryTypeName, factoryImplementationName.trim());
}
}
}
cache.put(classLoader, result);
return result;
}
catch (IOException ex) {
throw new IllegalArgumentException("Unable to load factories from location [" +
FACTORIES_RESOURCE_LOCATION + "]", ex);
}
}

SpringApplication 类 createSpringFactoriesInstances 方法,通过反射创建实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@SuppressWarnings("unchecked")
private <T> List<T> createSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes,
ClassLoader classLoader, Object[] args, Set<String> names) {
List<T> instances = new ArrayList<>(names.size());
for (String name : names) {
try {
Class<?> instanceClass = ClassUtils.forName(name, classLoader);
Assert.isAssignable(type, instanceClass);
Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes);
T instance = (T) BeanUtils.instantiateClass(constructor, args);
instances.add(instance);
}
catch (Throwable ex) {
throw new IllegalArgumentException("Cannot instantiate " + type + " : " + name, ex);
}
}
return instances;
}

blog-list

发表于 2021-06-18

优秀博客收集

北漂程序员,大量关于Java技术和面试题
chen_hao 源码分析系列
Java技术栈
老K的Java博客
Linux命令大全

柳年思水

技术电子书

eureka

发表于 2021-06-18

享学Eureka源码
https://fangshixiang.blog.csdn.net/article/details/105043241

芋道源码
https://github.com/YunaiV/eureka

eureka-client 最底层、最核心的一个包,微服务通过eureka-client与Eureka进行通信,屏蔽了底层通信细节。
eureka-core 依赖eureka-client。
eureka-server 依赖jersey 来搭建Servlet应用。

eureka-core 它不是所有模块的基础,而是仅仅用于Server端,并且还不是必须的。

Eureka Client

Eureka Client 是Java客户端,用于简化与Eureka Server通信细节,Eureka Client会拉取、更新和缓存Eureka Server中服务的信息。即使所有的Eureka Server 节点都宕机,短时间内服务消费者依然可以使用缓存中的信息找到服务提供者。

Eureka Client 在微服务启动后,会周期性的向Eureka Server发送心跳(默认周期为30秒),用来续约自己的信息。

如果Eureka Server在一定时间内(默认是90秒)没有收到某个微服务节点的心跳,Eureka Server将会注销该微服务节点。

Client端每30秒发送一次心跳,Server端90秒内没有收到该Client心跳就认为Client端挂掉了。

Eureka Client 完成以下几件动作:

  • 服务注册Register:Client端主动向Server端注册自身的元数据,比如IP地址、端口等;
  • 服务续约Renew:Client默认会每30秒发送一次心跳来续约,告诉Server自己还活着;
  • 获取注册列表信息Fetch Registries:Client端从Server端获取注册表信息,并将其缓存到本地。缓存信息每30秒更新一次;
  • 服务下线Cancel:Client端在停机时(正常停止)会主动向Server端发送取消请求,告诉Server端把自己这个实例剔除。
Eureka Server

Eureka Server 提供给各个微服务注册的服务端,Eureka Server 会在内存中存储该服务的信息。

Server端并不会主动触发动作,主要用于提供服务:

  • 提供服务注册
  • 提供服务信息拉取
  • 提供服务管理:接收客户端的cancel、心跳、续租renew等请求;
  • 服务剔除
  • 信息同步:在集群中,每个Eureka Server同时也是Eureka Client,多个Server之间通过P2P复制的方式完成服务注册表的同步。

注册中心作为微服务架构的核心中间件,对可用性是强要求,反而对一致性是可以容忍的,基于AP模式的只有Eureka 和 Nacos。

binary-search

发表于 2021-06-15

二分查找是一种非常简单易懂的查找算法。举个例子,比如猜数字游戏,我随机写一个0-99之间的数字,你来猜我写的是什么。猜的过程中,你每猜一次,我会告诉你猜大了还是猜小了,直到猜中为止。

有序数组,折半查找,缩小查找区间的范围

下面以升序排序的int数组为例,介绍二分查找过程,待查找的int数组如下

1
int[] arr = {1, 3, 4, 5, 6, 8, 10, 12, 13, 18, 22};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

public class BinarySearch {

public int binarySearch(int[] data, int value) {

int low = 0;
int high = data.length - 1;

while (low <= high) {
// int mid = (low + high) / 2;
int mid = low + (high - low) / 2;
int midVal = data[mid];
if(midVal == value) {
return mid;

} else if (value > midVal) {
low = mid + 1;
}else {
high = mid - 1;
}
}

// 没有找到,返回索引-1
return -1;
}
}

首先定义两个变量low 和 high 分别指向有序数组arr[]的第一个元素和最后一个元素的下标;

1、获取low和high中间元素的下标mid = (low + high) / 2;

2、判断要查找的元素值value和arr[mid] 是否相等,如果相等返回下标mid,查找结束;

3、如果不相等,则比较arr[mid]和value的大小;

4、如果value大于arr[mid],说明要查找的元素值在[mid+1, high]之间,则将low指向mid+1,继续步骤1、2;
5、同样的,如果value小于arr[mid],说明要查找的元素值在[low, mid-1]之间,则将high指向mid-1,继续步骤1、2;

6、当low > high 的时候,结束查找。

hashmap-interview

发表于 2021-05-22

说说HashMap的实现原理

  • HashMap底层数据结构

    • 什么是Hash表?
    • 什么是哈希冲突?如何解决
  • HashMap源码读过吗?

    • 核心成员变量有哪些知道吗?
    • 为什么到8转为红黑树,到6转为链表?
    • 插入和获取数据的过程清楚吗
    • JDK7 和 JDK8 中hash函数的区别?
  • JDK7 与 JDK8 中HashMap的区别?

HashMap的实现原理

本文关于HashMap的底层实现若无特殊说明都是基于JDK1.8的。

1、HashMap的底层数据结构是数组+链表(红黑树),它是基于hash算法实现的,通过put(key, value) 和 get(key) 方法存储和获取对象。
我们一般是这么使用HashMap的

1
2
Map<String, Object> map = new HashMap<>();
map.put("a", "first");

当调用HashMap的无参构造方法时,HashMap的底层的数组是没有初始化的。

1
2
3
4
5
6
7
/**
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
* (16) and the default load factor (0.75).
*/
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}

无参构造方法中只是对加载因子loadFactor初始化为默认值0.75,并没有对Node<K,V>[] table数组初始化。
(可以思考下HashMap中的加载因子为什么是0.75?)

当我们第一次调用put(key, value) 方法时,key-value在HashMap内部的数组和链表中是如何存储的呢
put方法内部首先根据key计算hash值,hash函数是如何计算的呢?是直接返回key的hashCode吗,当然不是啦!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* Computes key.hashCode() and spreads (XORs) higher bits of hash
* to lower. Because the table uses power-of-two masking, sets of
* hashes that vary only in bits above the current mask will
* always collide. (Among known examples are sets of Float keys
* holding consecutive whole numbers in small tables.) So we
* apply a transform that spreads the impact of higher bits
* downward. There is a tradeoff between speed, utility, and
* quality of bit-spreading. Because many common sets of hashes
* are already reasonably distributed (so don't benefit from
* spreading), and because we use trees to handle large sets of
* collisions in bins, we just XOR some shifted bits in the
* cheapest possible way to reduce systematic lossage, as well as
* to incorporate impact of the highest bits that would otherwise
* never be used in index calculations because of table bounds.
*/
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

key的hash值已经知道了,那么计算 hash & (table.length - 1) 结果就是key的hash值在数组中的位置bucket。
等等,数组table还没有初始化呢,那么在计算bucket之前应该先将table[]数组进行初始化,那么数组的长度应该是多少呢?

1
2
3
4
/**
* The default initial capacity - MUST be a power of two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

默认的初始容量16。

1
2
3
newCap = DEFAULT_INITIAL_CAPACITY;
Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;

Node[] 数组就这么被创建好了,是不是很简单(具体看源码中的resize()方法)。

数组创建好了,key在数组中对应的位置bucket也找到了,那么现在就该将key-value放入数组中了,该如何放入呢?
当然是将key-value封装成数组的元素类型Node了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* Basic hash bin node, used for most entries. (See below for
* TreeNode subclass, and in LinkedHashMap for its Entry subclass.)
*/
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;

Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
// 省略其他部分代码
}

Node类中包含4个属性,K key、V value、int hash、Node<K,V> next。
key、value就是我们要放入HashMap中的数据,hash就是我们上面计算出来的key的hash值,这个Node类型的next是干嘛的呢?
还记得HashMap底层的数据结构吗?数组 + 链表,next这个地方就是链表的实现。next指向与key的hash值相同的新Node。

根据key在数组中对应的位置bucket,获取bucket位置上的元素,如果该位置上没有元素,则直接将key-value封装成的Node放入数组中

1
2
tab = table
tab[i] = newNode(hash, key, value, null);

如果该位置上有元素,则比较key的值是否相等
如果key的值相等,则要更新key对应的vaule,将新的value覆盖旧的value;
如果key的值不相等,则说明发生了hash冲突。也就是说不同的key计算出的hash值相等。这个时候链表就开始起作用了。

JDK1.8中put方法源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>.)
*/
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}

/**
* Implements Map.put and related methods.
*
* @param hash hash for key
* @param key the key
* @param value the value to put
* @param onlyIfAbsent if true, don't change existing value
* @param evict if false, the table is in creation mode.
* @return previous value, or null if none
*/
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
// 初始化table
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
// 目标位置上没有元素,直接将key-value封装成的Node放入数组
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
// 目标位置上有元素,则比较key值是否相等
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
// key值相等
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
// key值不相等
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
// 遍历链表,找到链表的最后一个节点,将新的元素插入到链表的尾部(尾插法)
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
// 判断是否需要将链表转成红黑树
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
// 链表继续遍历
p = e;
}
}
if (e != null) { // existing mapping for key
// 目标位置上有元素,将新的value覆盖旧的value
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
// 数组扩容
resize();
afterNodeInsertion(evict);
return null;
}

2、当存储对象时,将key-value传递给put(key, value) 方法时,内部调用hash函数根据key的hashCode来计算hash值,
然后将key的hash值与数组长度执行按位与运算,找到数组中对应的位置来存储value。
如果该位置上没有元素,则直接将key、value以及hash值封装成Node对象放入数组中。
如果该位置上有元素,则调用equals方法比较key值是否相等,如果相等则覆盖value值,如果key值不相等,则发生了hash 碰撞。
HashMap 使用链表来解决碰撞问题,当key发生碰撞了,对象将会存储在链表的下一个节点中。每个链表节点中存储key-value对象。
也就是当两个不同的key的hash值相同时,它们会存储在同一个bucket位置的链表(JDK8链表长度大于8会将链表转成红黑树),取数据可通过key的equals方法来
找到正确的key-value。

3、当获取对象时,也是先计算key的hash值,找到数组中对应的位置,然后通过key的equals方法找到正确的key-value,然后返回对象的value。

JDK8之前的HashMap底层数据结构

JDK8以前HashMap的实现是数组 + 链表,它之所以有相当快的查询速度主要是因为它是通过计算key的hashCode来决定数组中存储的位置,而增删速度靠的是链表保证。
JDK1.8中用数组 + 链表 + 红黑树的结构来优化,链表长度大于8同时满足HashMap中数组长度大于64则变红黑树,长度小于6变回链表。

什么是Hash表

散列表(Hash Table,也叫哈希表),是根据key值而直接进行访问的数据结构。也就是说,它通过把key值映射到表中一个位置来访问记录,以加快查找速度。
这个映射函数叫做散列函数,存放记录的数组叫散列表。
hash表里可以存储元素的位置称为桶(bucket)。

什么是哈希冲突

即不同key值产生相同的hash地址,hash(key1) = hash(key2)

哈希冲突解决方案

1、开放地址法
2、链地址法
3、公共溢出区法
4、再散列法

mysql-optimize

发表于 2021-05-20 | 分类于 MySQL

MySQL 5.6.35 索引优化导致的死锁案例解析
https://mp.weixin.qq.com/s/T5e-gb0MXxjBwbjGg6jIMg

MySQL 加锁实际上是给索引加锁,而不是给数据加锁。

查看死锁日志
show engine innodb status\G;
找到 LATEST DETECTED DEADLOCK

MySQL 优化之 index merge(索引合并)
https://www.cnblogs.com/digdeep/p/4975977.html

MySQL使用存储过程批量插入百(千)万测试数据
https://blog.csdn.net/qq_36663951/article/details/78790919

index merge
https://dev.mysql.com/doc/refman/5.7/en/index-merge-optimization.html

jvm-classloader

发表于 2021-05-18

混合模式

(086 详解Class加载过程 02:06:00)
Java 解释执行,编译执行。

解释器
bytecode interpreter

JIT
Just In Time compiler

混合模式
混合使用解释器 + 热点代码编译
起始阶段采用解释执行

热点代码检测:
多次被调用的方法
多次被调用的循环
进行编译

检查热点代码,通过JIT编译器将方法编译成机器码的触发阈值
-XX:CompileThreshold=10000

hashmap

发表于 2021-05-14

https://tech.meituan.com/2016/06/24/java-hashmap.html

比较 HashMap HashTable
key/value 是否为null key 和 value 都可以为null key 和 value 都不可以为null
是否同步 不同步 同步
说一下HashMap的实现原理

HashMap的数据结构:数组 + 链表(红黑树),HashMap基于hash算法实现的,当我们往HashMap中put元素时,利用key的hashCode重新hash计算出当前对象的元素在数组中的下标。
存储时,如果出现hash值相同的key,此时有两种情况:
1.如果key相同,则覆盖原来的值;
2.如果key不同,也就是出现了冲突,则将当前的key-value放入链表中。

根据key获取时,同样是利用key的hashCode计算hash值对应的下标,再进一步判断key是否相同,从而找到对应值。

HashMap解决hash冲突的方式就是使用数组的存储方式,将冲突的key的对象放入链表中,一旦发现冲突就在链表中做进一步的对比。

在JDK1.8中对HashMap的实现做了优化,当链表中的节点数超过8个之后,该链表会转换为红黑树来提高查询效率,从原来的O(N)到O(logN)。

影响HashMap性能的两个参数

  • initial capacity 初始容量,默认值 16
  • load factor 负载因子,默认值 0.75
成员变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 默认的初始容量16
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

// 最大容量
static final int MAXIMUM_CAPACITY = 1 << 30;

// 默认加载因子0.75
static final float DEFAULT_LOAD_FACTOR = 0.75f;

// Entry数组
static final Entry<?,?>[] EMPTY_TABLE = {};
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;

// map 中键值对数量
transient int size;

// 阈值,Entry数组超过 threshold 时进行扩容
int threshold;

// 加载因子
final float loadFactor;
构造方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

/**
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
* (16) and the default load factor (0.75).
*/
public HashMap() {
// 使用默认的初始容量16 和 默认的加载因子0.75 构造HashMap
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
}


/**
* Constructs an empty <tt>HashMap</tt> with the specified initial
* capacity and load factor.
*
* @param initialCapacity the initial capacity
* @param loadFactor the load factor
* @throws IllegalArgumentException if the initial capacity is negative
* or the load factor is nonpositive
*/
public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
// 加载因子
this.loadFactor = loadFactor;
// 将阈值设置为初始容量
threshold = initialCapacity;
// init 方法由子类实现
init();
}
添加元素
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>.)
*/
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
// 初始化Entry数组
inflateTable(threshold);
}
// 处理key值为null的情况
if (key == null)
return putForNullKey(value);
// 计算hash值
int hash = hash(key);
// 计算hash值在Entry数组中对应的索引
int i = indexFor(hash, table.length);
// 取出索引i对应的Entry值,判断Entry是否为null,如果Entry有值,遍历Entry链表
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
// hash值相等 并且 key值相等
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}

modCount++;
// 添加新的元素
addEntry(hash, key, value, i);
return null;
}
1
2
3
4
5
6
7
8
9
10
11
12
/**
* Inflates the table.
*/
private void inflateTable(int toSize) {
// Find a power of 2 >= toSize 找到一个大于等于toSize的2的N次幂
int capacity = roundUpToPowerOf2(toSize);
// 计算阈值 threshold = capacity * loadFactor 容量乘以加载因子
threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
// 初始化Entry数组
table = new Entry[capacity];
initHashSeedAsNeeded(capacity);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 /**
* Adds a new entry with the specified key, value and hash code to
* the specified bucket. It is the responsibility of this
* method to resize the table if appropriate.
*
* Subclass overrides this to alter the behavior of put method.
*/
void addEntry(int hash, K key, V value, int bucketIndex) {
// 判断是否需要扩容
if ((size >= threshold) && (null != table[bucketIndex])) {
// Entry数组扩容为原来的2倍
resize(2 * table.length);
// 计算hash值,如果key为null 则hash值为0
hash = (null != key) ? hash(key) : 0;
// 计算hash值的索引
bucketIndex = indexFor(hash, table.length);
}

createEntry(hash, key, value, bucketIndex);
}

/**
* Like addEntry except that this version is used when creating entries
* as part of Map construction or "pseudo-construction" (cloning,
* deserialization). This version needn't worry about resizing the table.
*
* Subclass overrides this to alter the behavior of HashMap(Map),
* clone, and readObject.
*/
void createEntry(int hash, K key, V value, int bucketIndex) {
// 先取出目标索引位置的值
Entry<K,V> e = table[bucketIndex];
// 头插法,将新的Entry键值对插入链表头部,并将索引位置的值添加到新值后面(next属性)
table[bucketIndex] = new Entry<>(hash, key, value, e);
size++;
}

HashMap 是数组+链表数据结构组成的,那么数组的类型是什么

计算hash值
计算元素在数组中的位置
数组的长度
数组的类型Entry

Entry 类型的数组 和 Entry类型的链表
hash
key
value
next

如果key 值相同,hash值肯定相同,则直接替换值。
如果hash 值相同,key 不同,则放在链表中。

发生hash冲突时,jdk1.7头插法,jdk1.8尾插法。

阈值
threshold = capacity * load factor

阅读全文 »

ThreadLocal

发表于 2021-05-14

ThreadLocal

ThreadLocalMap 是 ThreadLocal 的静态内部类。

Thread 类的成员变量 ThreadLocal.ThreadLocalMap threadLocals。

ThreadLocalMap 成员变量Entry[] ,Entry 继承 WeakReference<ThreadLocal<?>>。

Entry 的key 是 ThreadLocal,value 是 Object。

1
2
3
4
ThreadLocal<List<Integer>> tl = new ThreadLocal<>();
List<Integer> cacheInstance = new ArrayList<>(10000);
tl.set(cacheInstance);
tl = new ThreadLocal<>();
123…11

geekymv

110 日志
8 分类
23 标签
© 2022 geekymv
由 Hexo 强力驱动
| 总访问量次 | 总访客人 |
主题 — NexT.Muse v5.1.4