Skip to content

Commit 60ed4f4

Browse files
committed
auto commit
1 parent 8a9b8b1 commit 60ed4f4

File tree

6 files changed

+32
-19
lines changed

6 files changed

+32
-19
lines changed

notes/Java IO.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,17 @@ public static void listAllFiles(File dir) {
6969

7070
```java
7171
public static void copyFile(String src, String dist) throws IOException {
72-
7372
FileInputStream in = new FileInputStream(src);
7473
FileOutputStream out = new FileOutputStream(dist);
74+
7575
byte[] buffer = new byte[20 * 1024];
76+
int cnt;
7677

7778
// read() 最多读取 buffer.length 个字节
7879
// 返回的是实际读取的个数
7980
// 返回 -1 的时候表示读到 eof,即文件尾
80-
while (in.read(buffer, 0, buffer.length) != -1) {
81-
out.write(buffer);
81+
while ((cnt = in.read(buffer, 0, buffer.length)) != -1) {
82+
out.write(buffer, 0, cnt);
8283
}
8384

8485
in.close();

notes/Java 基础.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ String 不可变性天生具备线程安全,可以在多个线程中安全地
196196

197197
## String Pool
198198

199-
字符串常量池(String Poll)保存着所有字符串字面量(literal strings),这些字面量在编译时期就确定。不仅如此,还可以使用 Stringintern() 方法在运行过程中将字符串添加到 String Poll 中。
199+
字符串常量池(String Pool)保存着所有字符串字面量(literal strings),这些字面量在编译时期就确定。不仅如此,还可以使用 Stringintern() 方法在运行过程中将字符串添加到 String Pool 中。
200200

201-
当一个字符串调用 intern() 方法时,如果 String Poll 中已经存在一个字符串和该字符串值相等(使用 equals() 方法进行确定),那么就会返回 String Poll 中字符串的引用;否则,就会在 String Poll 中添加一个新的字符串,并返回这个新字符串的引用。
201+
当一个字符串调用 intern() 方法时,如果 String Pool 中已经存在一个字符串和该字符串值相等(使用 equals() 方法进行确定),那么就会返回 String Pool 中字符串的引用;否则,就会在 String Pool 中添加一个新的字符串,并返回这个新字符串的引用。
202202

203203
下面示例中,s1 和 s2 采用 new String() 的方式新建了两个不同字符串,而 s3 和 s4 是通过 s1.intern() 方法取得一个字符串引用。intern() 首先把 s1 引用的字符串放到 String Pool 中,然后返回这个字符串引用。因此 s3 和 s4 引用的是同一个字符串。
204204

@@ -216,19 +216,19 @@ System.out.println(s3 == s4); // true
216216
```java
217217
String s5 = "bbb";
218218
String s6 = "bbb";
219-
System.out.println(s4 == s5); // true
219+
System.out.println(s5 == s6); // true
220220
```
221221

222-
Java 7 之前,String Poll 被放在运行时常量池中,它属于永久代。而在 Java 7String Poll 被移到堆中。这是因为永久代的空间有限,在大量使用字符串的场景下会导致 OutOfMemoryError 错误。
222+
Java 7 之前,String Pool 被放在运行时常量池中,它属于永久代。而在 Java 7String Pool 被移到堆中。这是因为永久代的空间有限,在大量使用字符串的场景下会导致 OutOfMemoryError 错误。
223223

224224
- [StackOverflow : What is String interning?](https://stackoverflow.com/questions/10578984/what-is-string-interning)
225225
- [深入解析 String#intern](https://tech.meituan.com/in_depth_understanding_string_intern.html)
226226

227227
## new String("abc")
228228

229-
使用这种方式一共会创建两个字符串对象(前提是 String Poll 中还没有 "abc" 字符串对象)。
229+
使用这种方式一共会创建两个字符串对象(前提是 String Pool 中还没有 "abc" 字符串对象)。
230230

231-
- "abc" 属于字符串字面量,因此编译时期会在 String Poll 中创建一个字符串对象,指向这个 "abc" 字符串字面量;
231+
- "abc" 属于字符串字面量,因此编译时期会在 String Pool 中创建一个字符串对象,指向这个 "abc" 字符串字面量;
232232
- 而使用 new 的方式会在堆中创建一个字符串对象。
233233

234234
创建一个测试类,其 main 方法中使用这种方式来创建字符串对象。
@@ -267,7 +267,7 @@ Constant pool:
267267
// ...
268268
```
269269

270-
Constant Poll 中,#19 存储这字符串字面量 "abc",#3String Poll 的字符串对象,它指向 #19 这个字符串字面量。在 main 方法中,0: 行使用 new #2 在堆中创建一个字符串对象,并且使用 ldc #3 将 String Poll 中的字符串对象作为 String 构造函数的参数。
270+
Constant Pool 中,#19 存储这字符串字面量 "abc",#3String Pool 的字符串对象,它指向 #19 这个字符串字面量。在 main 方法中,0: 行使用 new #2 在堆中创建一个字符串对象,并且使用 ldc #3 将 String Pool 中的字符串对象作为 String 构造函数的参数。
271271

272272
以下是 String 构造函数的源码,可以看到,在将一个字符串对象作为另一个字符串对象的构造函数参数时,并不会完全复制 value 数组内容,而是都会指向同一个 value 数组。
273273

@@ -368,10 +368,11 @@ short s1 = 1;
368368
// s1 = s1 + 1;
369369
```
370370

371-
但是使用 += 运算符可以执行隐式类型转换。
371+
但是使用 += 或者 ++ 运算符可以执行隐式类型转换。
372372

373373
```java
374374
s1 += 1;
375+
// s1++;
375376
```
376377

377378
上面的语句相当于将 s1 + 1 的计算结果进行了向下转型:
@@ -1042,6 +1043,10 @@ private 方法隐式地被指定为 final,如果在子类中定义的方法和
10421043

10431044
声明类不允许被继承。
10441045

1046+
**4. 构造器**
1047+
1048+
声明类不允许被 `new` 实例化,多用于 `Singleton` 模式中。如果该类有子类需要继承,若该类无其他构造器,则不允许被继承。
1049+
10451050
## static
10461051

10471052
**1. 静态变量**

notes/Java 并发.md

+5
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ java.util.concurrent(J.U.C)大大提高了并发性能,AQS 被认为是 J.
740740

741741
```java
742742
public class CountdownLatchExample {
743+
743744
public static void main(String[] args) throws InterruptedException {
744745
final int totalThread = 10;
745746
CountDownLatch countDownLatch = new CountDownLatch(totalThread);
@@ -788,6 +789,7 @@ public CyclicBarrier(int parties) {
788789

789790
```java
790791
public class CyclicBarrierExample {
792+
791793
public static void main(String[] args) {
792794
final int totalThread = 10;
793795
CyclicBarrier cyclicBarrier = new CyclicBarrier(totalThread);
@@ -822,6 +824,7 @@ Semaphore 类似于操作系统中的信号量,可以控制对互斥资源的
822824

823825
```java
824826
public class SemaphoreExample {
827+
825828
public static void main(String[] args) {
826829
final int clientCount = 3;
827830
final int totalRequestCount = 10;
@@ -866,6 +869,7 @@ FutureTask 可用于异步获取执行结果或取消执行任务的场景。当
866869

867870
```java
868871
public class FutureTaskExample {
872+
869873
public static void main(String[] args) throws ExecutionException, InterruptedException {
870874
FutureTask<Integer> futureTask = new FutureTask<Integer>(new Callable<Integer>() {
871875
@Override
@@ -971,6 +975,7 @@ produce..produce..consume..consume..produce..consume..produce..consume..produce.
971975

972976
```java
973977
public class ForkJoinExample extends RecursiveTask<Integer> {
978+
974979
private final int threshold = 5;
975980
private int first;
976981
private int last;

notes/剑指 offer 题解.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,7 @@ public ListNode FindKthToTail(ListNode head, int k) {
11541154

11551155
## 解题思路
11561156

1157-
使用双指针,一个指针 fast 每次移动两个节点,一个指针 slow 每次移动一个节点。因为存在环,所以两个指针必定相遇在环中的某个节点上。假设相遇点在下图的 z1 位置,此时 fast 移动的节点数为 x+2y+z,slow 为 x+y,由于 fast 速度比 slow 快一倍,因此 x+2y+z=2(x+y),得到 x=z。
1157+
使用双指针,一个指针 fast 每次移动两个节点,一个指针 slow 每次移动一个节点。因为存在环,所以两个指针必定相遇在环中的某个节点上。假设相遇点在下图的 y4 位置,此时 fast 移动的节点数为 x+2y+z,slow 为 x+y,由于 fast 速度比 slow 快一倍,因此 x+2y+z=2(x+y),得到 x=z。
11581158

11591159
在相遇点,slow 要到环的入口点还需要移动 z 个节点,如果让 fast 重新从头开始移动,并且速度变为每次移动一个节点,那么它到环入口点还需要移动 x 个节点。在上面已经推导出 x=z,因此 fast 和 slow 将在环入口点相遇。
11601160

notes/数据库系统原理.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,12 @@ SELECT ... FOR UPDATE;
294294

295295
----
296296

297-
| 隔离级别 | 脏读 | 不可重复读 | 幻影读 |
298-
| :---: | :---: | :---:| :---: |
299-
| 未提交读 ||||
300-
| 提交读 | × |||
301-
| 可重复读 | × | × ||
302-
| 可串行化 | × | × | × |
297+
| 隔离级别 | 脏读 | 不可重复读 | 幻影读 | 加锁读 |
298+
| :---: | :---: | :---:| :---: | :---: |
299+
| 未提交读 |||| × |
300+
| 提交读 | × ||| × |
301+
| 可重复读 | × | × || × |
302+
| 可串行化 | × | × | × ||
303303

304304
# 五、多版本并发控制
305305

notes/缓存.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public class LRU<K, V> implements Iterable<K> {
9999

100100
if (map.size() > maxSize) {
101101
Node toRemove = removeTail();
102-
map.remove(toRemove);
102+
map.remove(toRemove.k);
103103
}
104104
}
105105

@@ -114,6 +114,7 @@ public class LRU<K, V> implements Iterable<K> {
114114

115115
private void appendHead(Node node) {
116116
node.next = head.next;
117+
node.next.pre = node;
117118
node.pre = head;
118119
head.next = node;
119120
}
@@ -122,6 +123,7 @@ public class LRU<K, V> implements Iterable<K> {
122123
private Node removeTail() {
123124
Node node = tail.pre;
124125
tail.pre = node.pre;
126+
node.pre.next = tail;
125127
return node;
126128
}
127129

0 commit comments

Comments
 (0)