GeezerVison is showing github.com — Merge branch 'Snailclimb:master' into master · zjtech/JavaGuide@5438529
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/java/basis/Java基础知识.md +2-3Lines changed: 2 additions & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1119,7 +1119,6 @@ abstract class AbstractStringBuilder implements Appendable, CharSequence { | 1119 | 1119 | Object 类是一个特殊的类,是所有类的父类。它主要提供了以下 11 个方法: | 1120 | 1120 |
| 1121 | 1121 | ```java | 1122 | | - | 1123 | 1122 | public final native Class<?> getClass()//native方法,用于返回当前运行时对象的Class对象,使用了final关键字修饰,故不允许子类重写。 | 1124 | 1123 |
| 1125 | 1124 | public native int hashCode() //native方法,用于返回对象的哈希码,主要使用在哈希表中,比如JDK中的HashMap。 | @@ -1140,9 +1139,9 @@ public final void wait(long timeout, int nanos) throws InterruptedException//多 | 1140 | 1139 | public final void wait() throws InterruptedException//跟之前的2个wait方法一样,只不过该方法一直等待,没有超时时间这个概念 | 1141 | 1140 |
| 1142 | 1141 | protected void finalize() throws Throwable { }//实例被垃圾回收器回收的时候触发的操作 | 1143 | | - | 1144 | 1142 | ``` | 1145 | 1143 |
| | 1144 | + | 1146 | 1145 | ## 反射 | 1147 | 1146 |
| 1148 | 1147 | ### 何为反射? | @@ -1400,4 +1399,4 @@ Java Io 流共涉及 40 多个类,这些类看上去很杂乱,但实际上 | 1400 | 1399 |
| 1401 | 1400 | - https://stackoverflow.com/questions/1906445/what-is-the-difference-between-jdk-and-jre | 1402 | 1401 | - https://www.educba.com/oracle-vs-openjdk/ | 1403 | | -- https://stackoverflow.com/questions/22358071/differences-between-oracle-jdk-and-openjdk?answertab=active#tab-top## 基础概念与常识 | | 1402 | +- https://stackoverflow.com/questions/22358071/differences-between-oracle-jdk-and-openjdk?answertab=active#tab-top## 基础概念与常识 |
Collapse file Copy file name to clipboardExpand all lines: docs/java/basis/Java基础知识疑难点.md +8-8Lines changed: 8 additions & 8 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -72,16 +72,16 @@ Reference:[Java中equals方法造成空指针异常的原因及解决方案](htt | 72 | 72 | 先看下面这个例子: | 73 | 73 |
| 74 | 74 | ```java | 75 | | -Integer x = 3; | 76 | | -Integer y = 3; | 77 | | -System.out.println(x == y);// true | 78 | | -Integer a = new Integer(3); | 79 | | -Integer b = new Integer(3); | 80 | | -System.out.println(a == b);//false | 81 | | -System.out.println(a.equals(b));//true | | 75 | +Integer i1 = 40; | | 76 | +Integer i2 = new Integer(40); | | 77 | +System.out.println(i1==i2);//false | 82 | 78 | ``` | 83 | 79 |
| 84 | | -当使用自动装箱方式创建一个Integer对象时,当数值在-128 ~127时,会将创建的 Integer 对象缓存起来,当下次再出现该数值时,直接从缓存中取出对应的Integer对象。所以上述代码中,x和y引用的是相同的Integer对象。 | | 80 | +`Integer i1=40` 这一行代码会发生拆箱,也就是说这行代码等价于 `Integer i1=Integer.valueOf(40)` 。因此,`i1` 直接使用的是常量池中的对象。而`Integer i1 = new Integer(40)` 会直接创建新的对象。因此,输出 false 。 | | 81 | + | | 82 | +记住:**所有整型包装类对象之间值的比较,全部使用 `equals()` 方法比较**。 | | 83 | + | | 84 | + | 85 | 85 |
| 86 | 86 | **注意:** 如果你的IDE(IDEA/Eclipse)上安装了阿里巴巴的p3c插件,这个插件如果检测到你用 ==的话会报错提示,推荐安装一个这个插件,很不错。 | 87 | 87 |
| |
Collapse file Copy file name to clipboardExpand all lines: docs/java/basis/反射机制.md +2-2Lines changed: 2 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -80,7 +80,7 @@ Class alunbarClass2 = o.getClass(); | 80 | 80 | **4.通过类加载器`xxxClassLoader.loadClass()`传入类路径获取:** | 81 | 81 |
| 82 | 82 | ```java | 83 | | -class clazz = ClassLoader.LoadClass("cn.javaguide.TargetObject"); | | 83 | +Class clazz = ClassLoader.loadClass("cn.javaguide.TargetObject"); | 84 | 84 | ``` | 85 | 85 |
| 86 | 86 | 通过类加载器获取 Class 对象不会进行初始化,意味着不进行包括初始化等一些列步骤,静态块和静态对象不会得到执行 | @@ -173,4 +173,4 @@ value is JavaGuide | 173 | 173 |
| 174 | 174 | ```java | 175 | 175 | Class<?> tagetClass = Class.forName("cn.javaguide.TargetObject"); | 176 | | -``` | | 176 | +``` |
Collapse file Copy file name to clipboardExpand all lines: docs/java/collection/ArrayList源码+扩容机制分析.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -894,7 +894,7 @@ public class EnsureCapacityTest { | 894 | 894 | 运行结果: | 895 | 895 |
| 896 | 896 | ``` | 897 | | -使用ensureCapacity方法前:1773 | | 897 | +使用ensureCapacity方法后:1773 | 898 | 898 | ``` | 899 | 899 |
| 900 | 900 | 通过运行结果,我们可以看出向 ArrayList 添加大量元素之前最好先使用`ensureCapacity` 方法,以减少增量重新分配的次数。 |
Collapse file Copy file name to clipboardExpand all lines: docs/java/collection/ConcurrentHashMap源码+底层数据结构分析.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -91,7 +91,7 @@ public ConcurrentHashMap(int initialCapacity,float loadFactor, int concurrencyLe | 91 | 91 | 总结一下在 Java 7 中 ConcurrnetHashMap 的初始化逻辑。 | 92 | 92 |
| 93 | 93 | 1. 必要参数校验。 | 94 | | -2. 校验并发级别 concurrencyLevel 大小,如果大于最大值,重置为最大值。无惨构造**默认值是 16.** | | 94 | +2. 校验并发级别 concurrencyLevel 大小,如果大于最大值,重置为最大值。无参构造**默认值是 16.** | 95 | 95 | 3. 寻找并发级别 concurrencyLevel 之上最近的 **2 的幂次方**值,作为初始化容量大小,**默认是 16**。 | 96 | 96 | 4. 记录 segmentShift 偏移量,这个值为【容量 = 2 的N次方】中的 N,在后面 Put 时计算位置时会用到。**默认是 32 - sshift = 28**. | 97 | 97 | 5. 记录 segmentMask,默认是 ssize - 1 = 16 -1 = 15. | |
Collapse file Copy file name to clipboardExpand all lines: docs/java/collection/Java集合框架常见面试题.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 86 | 86 |
| 87 | 87 | - `HashMap`: JDK1.8 之前 `HashMap` 由数组+链表组成的,数组是 `HashMap` 的主体,链表则是主要为了解决哈希冲突而存在的(“拉链法”解决冲突)。JDK1.8 以后在解决哈希冲突时有了较大的变化,当链表长度大于阈值(默认为 8)(将链表转换成红黑树前会判断,如果当前数组的长度小于 64,那么会选择先进行数组扩容,而不是转换为红黑树)时,将链表转化为红黑树,以减少搜索时间 | 88 | 88 | - `LinkedHashMap`: `LinkedHashMap` 继承自 `HashMap`,所以它的底层仍然是基于拉链式散列结构即由数组和链表或红黑树组成。另外,`LinkedHashMap` 在上面结构的基础上,增加了一条双向链表,使得上面的结构可以保持键值对的插入顺序。同时通过对链表进行相应的操作,实现了访问顺序相关逻辑。详细可以查看:[《LinkedHashMap 源码详细分析(JDK1.8)》](https://www.imooc.com/article/22931) | 89 | | -- `Hashtable`: 数组+链表组成的,数组是 `HashMap` 的主体,链表则是主要为了解决哈希冲突而存在的 | | 89 | +- `Hashtable`: 数组+链表组成的,数组是 `Hashtable` 的主体,链表则是主要为了解决哈希冲突而存在的 | 90 | 90 | - `TreeMap`: 红黑树(自平衡的排序二叉树) | 91 | 91 |
| 92 | 92 | ### 1.1.4. 如何选用集合? | |
Collapse file Copy file name to clipboardExpand all lines: docs/java/jvm/JVM垃圾回收.md +20-16Lines changed: 20 additions & 16 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -176,26 +176,30 @@ public class GCTest { | 176 | 176 |
| 177 | 177 | 大部分情况,对象都会首先在 Eden 区域分配,在一次新生代垃圾回收后,如果对象还存活,则会进入 s0 或者 s1,并且对象的年龄还会加 1(Eden 区->Survivor 区后对象的初始年龄变为 1),当它的年龄增加到一定程度(默认为 15 岁),就会被晋升到老年代中。对象晋升到老年代的年龄阈值,可以通过参数 `-XX:MaxTenuringThreshold` 来设置。 | 178 | 178 |
| 179 | | -> 修正([issue552](https://github.com/Snailclimb/JavaGuide/issues/552)):“Hotspot 遍历所有对象时,按照年龄从小到大对其所占用的大小进行累积,当累积的某个年龄大小超过了 survivor 区的一半时,取这个年龄和 MaxTenuringThreshold 中更小的一个值,作为新的晋升年龄阈值”。 | | 179 | +> 修正([issue552](https://github.com/Snailclimb/JavaGuide/issues/552)):“Hotspot 遍历所有对象时,按照年龄从小到大对其所占用的大小进行累积,当累积的某个年龄大小超过了 survivor 区的 50% 时(默认值是 50%,可以通过 `-XX:TargetSurvivorRatio=percent` 来设置,参见 [issue1199](https://github.com/Snailclimb/JavaGuide/issues/1199) ),取这个年龄和 MaxTenuringThreshold 中更小的一个值,作为新的晋升年龄阈值”。 | 180 | 180 | > | 181 | | -> **动态年龄计算的代码如下** | | 181 | +> jdk8官方文档引用 :https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html 。 | | 182 | +> | | 183 | +>  | | 184 | +> | | 185 | +> **动态年龄计算的代码如下:** | 182 | 186 | > | 183 | 187 | > ```c++ | 184 | 188 | > uint ageTable::compute_tenuring_threshold(size_t survivor_capacity) { | 185 | | -> //survivor_capacity是survivor空间的大小 | 186 | | -> size_t desired_survivor_size = (size_t)((((double)survivor_capacity)*TargetSurvivorRatio)/100); | 187 | | -> size_t total = 0; | 188 | | -> uint age = 1; | 189 | | -> while (age < table_size) { | 190 | | -> //sizes数组是每个年龄段对象大小 | 191 | | -> total += sizes[age]; | 192 | | -> if (total > desired_survivor_size) { | 193 | | -> break; | 194 | | -> } | 195 | | -> age++; | 196 | | -> } | 197 | | -> uint result = age < MaxTenuringThreshold ? age : MaxTenuringThreshold; | 198 | | -> ... | | 189 | +> //survivor_capacity是survivor空间的大小 | | 190 | +> size_t desired_survivor_size = (size_t)((((double)survivor_capacity)*TargetSurvivorRatio)/100); | | 191 | +> size_t total = 0; | | 192 | +> uint age = 1; | | 193 | +> while (age < table_size) { | | 194 | +> //sizes数组是每个年龄段对象大小 | | 195 | +> total += sizes[age]; | | 196 | +> if (total > desired_survivor_size) { | | 197 | +> break; | | 198 | +> } | | 199 | +> age++; | | 200 | +> } | | 201 | +> uint result = age < MaxTenuringThreshold ? age : MaxTenuringThreshold; | | 202 | +> ... | 199 | 203 | > } | 200 | 204 | > | 201 | 205 | > ``` | |
Collapse file Copy file name to clipboardExpand all lines: docs/java/multi-thread/java线程池学习总结.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -545,7 +545,7 @@ public interface Callable<V> { | 545 | 545 | 1. **`execute()`方法用于提交不需要返回值的任务,所以无法判断任务是否被线程池执行成功与否;** | 546 | 546 | 2. **`submit()`方法用于提交需要返回值的任务。线程池会返回一个 `Future` 类型的对象,通过这个 `Future` 对象可以判断任务是否执行成功** ,并且可以通过 `Future` 的 `get()`方法来获取返回值,`get()`方法会阻塞当前线程直到任务完成,而使用 `get(long timeout,TimeUnit unit)`方法则会阻塞当前线程一段时间后立即返回,这时候有可能任务没有执行完。 | 547 | 547 |
| 548 | | -我们以**`AbstractExecutorService`**接口中的一个 `submit` 方法为例子来看看源代码: | | 548 | +我们以 **`AbstractExecutorService`** 接口中的一个 `submit()` 方法为例子来看看源代码: | 549 | 549 |
| 550 | 550 | ```java | 551 | 551 | public Future<?> submit(Runnable task) { | |
|