HashMap源码
当
size表示当前hashmap里面已经包含的元素的个数。
当调用put方法插入时,如果该key已经存在,则在替换value后,不会++size
if (++size > threshold){ resize();}
其中
size:The number of key-value mappings contained in this map.和上面的是一样的
threshold:newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
也是一样的,
最后总结一下:就是这个map里面包含的元素,也就是size的值,大于这个阈值的时候,才会resize(),扩容是两位扩容,如果发现插入;
mybatis一级缓存
session级别的缓存,如果调用的sql参数一样, 根据sql用hash算出的key也是一样,查询出result后,按当前的key放出缓存里,下次从缓存里取出结果
二级缓存是sqlSessionFactory级别的,需要开启
先从二级缓存里取,不存在再从一级里取,在session关闭后,二级缓存提交事务,才会生效,sqlsession不关闭,二级缓存不会生效,
jdk
package sun.misc 下的Launcher类 是classLoader的一个启动类,
appClassLoader,ExtClassLoader类加载器都是它的内部类,里面代码用System.getProperty(String path)分别加载了不同的路径下的class
System.getProperty("sun.boot.class.path"); System.getProperty("java.class.path"); System.getProperty("java.ext.dirs");
ClassLoader classLoader = LoadClassByHand.class.getClassLoader();
Class<?> aClass = classLoader.loadClass("jvm.com.LoadClassByHand");
classLoader类源码,loadClass(String name) 调用重载的方法
public Class<?> loadClass(String name) throws ClassNotFoundException {
return loadClass(name, false);
}
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) { //加载的时候加锁
// First, check if the class has already been loaded //先检查是否已经加载进来了 native方法虚拟机实现
Class<?> c = findLoadedClass(name);
if (c == null) {
long t0 = System.nanoTime();
try {
if (parent != null) { // final ClassLoader parent;没有加载进来 ,调用partent的load方法,类似递归,父类上来也是执行查找缓存,没有加载进来找父类
c = parent.loadClass(name, false);
} else { // 没有父加载器了 bootstrap加载器从已加载的class里找,没找着返回null
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
if (c == null) { //如果parent都没有找到,只能自己去加载,调用findClass方法
// If still not found, then invoke findClass in order
// to find the class.
long t1 = System.nanoTime();
c = findClass(name);
// this is the defining class loader; record the stats
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
}
父类定义好了逻辑,留了个方法,留给子类去实现 设计模式模板方法
//自定义ClassLoader继承 ClassLoader,需要重写findClass方法
protected Class<?> findClass(String name) throws ClassNotFoundException {
throw new ClassNotFoundException(name);
}