Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wushizhenking
CS-Notes
提交
60ed4f49
C
CS-Notes
项目概览
wushizhenking
/
CS-Notes
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
CS-Notes
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
60ed4f49
编写于
9月 14, 2018
作者:
C
CyC2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
auto commit
上级
8a9b8b15
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
32 addition
and
19 deletion
+32
-19
notes/Java IO.md
notes/Java IO.md
+4
-3
notes/Java 基础.md
notes/Java 基础.md
+13
-8
notes/Java 并发.md
notes/Java 并发.md
+5
-0
notes/剑指 offer 题解.md
notes/剑指 offer 题解.md
+1
-1
notes/数据库系统原理.md
notes/数据库系统原理.md
+6
-6
notes/缓存.md
notes/缓存.md
+3
-1
未找到文件。
notes/Java IO.md
浏览文件 @
60ed4f49
...
@@ -69,16 +69,17 @@ public static void listAllFiles(File dir) {
...
@@ -69,16 +69,17 @@ public static void listAllFiles(File dir) {
```
java
```
java
public
static
void
copyFile
(
String
src
,
String
dist
)
throws
IOException
{
public
static
void
copyFile
(
String
src
,
String
dist
)
throws
IOException
{
FileInputStream
in
=
new
FileInputStream
(
src
);
FileInputStream
in
=
new
FileInputStream
(
src
);
FileOutputStream
out
=
new
FileOutputStream
(
dist
);
FileOutputStream
out
=
new
FileOutputStream
(
dist
);
byte
[]
buffer
=
new
byte
[
20
*
1024
];
byte
[]
buffer
=
new
byte
[
20
*
1024
];
int
cnt
;
// read() 最多读取 buffer.length 个字节
// read() 最多读取 buffer.length 个字节
// 返回的是实际读取的个数
// 返回的是实际读取的个数
// 返回 -1 的时候表示读到 eof,即文件尾
// 返回 -1 的时候表示读到 eof,即文件尾
while
(
in
.
read
(
buffer
,
0
,
buffer
.
length
)
!=
-
1
)
{
while
(
(
cnt
=
in
.
read
(
buffer
,
0
,
buffer
.
length
)
)
!=
-
1
)
{
out
.
write
(
buffer
);
out
.
write
(
buffer
,
0
,
cnt
);
}
}
in
.
close
();
in
.
close
();
...
...
notes/Java 基础.md
浏览文件 @
60ed4f49
...
@@ -196,9 +196,9 @@ String 不可变性天生具备线程安全,可以在多个线程中安全地
...
@@ -196,9 +196,9 @@ String 不可变性天生具备线程安全,可以在多个线程中安全地
## String Pool
## String Pool
字符串常量池(String Po
ll)保存着所有字符串字面量(literal strings),这些字面量在编译时期就确定。不仅如此,还可以使用 String 的 intern() 方法在运行过程中将字符串添加到 String Pol
l 中。
字符串常量池(String Po
ol)保存着所有字符串字面量(literal strings),这些字面量在编译时期就确定。不仅如此,还可以使用 String 的 intern() 方法在运行过程中将字符串添加到 String Poo
l 中。
当一个字符串调用 intern() 方法时,如果 String Po
ll 中已经存在一个字符串和该字符串值相等(使用 equals() 方法进行确定),那么就会返回 String Poll 中字符串的引用;否则,就会在 String Pol
l 中添加一个新的字符串,并返回这个新字符串的引用。
当一个字符串调用 intern() 方法时,如果 String Po
ol 中已经存在一个字符串和该字符串值相等(使用 equals() 方法进行确定),那么就会返回 String Pool 中字符串的引用;否则,就会在 String Poo
l 中添加一个新的字符串,并返回这个新字符串的引用。
下面示例中,s1 和 s2 采用 new String() 的方式新建了两个不同字符串,而 s3 和 s4 是通过 s1.intern() 方法取得一个字符串引用。intern() 首先把 s1 引用的字符串放到 String Pool 中,然后返回这个字符串引用。因此 s3 和 s4 引用的是同一个字符串。
下面示例中,s1 和 s2 采用 new String() 的方式新建了两个不同字符串,而 s3 和 s4 是通过 s1.intern() 方法取得一个字符串引用。intern() 首先把 s1 引用的字符串放到 String Pool 中,然后返回这个字符串引用。因此 s3 和 s4 引用的是同一个字符串。
...
@@ -216,19 +216,19 @@ System.out.println(s3 == s4); // true
...
@@ -216,19 +216,19 @@ System.out.println(s3 == s4); // true
```
java
```
java
String
s5
=
"bbb"
;
String
s5
=
"bbb"
;
String
s6
=
"bbb"
;
String
s6
=
"bbb"
;
System
.
out
.
println
(
s
4
==
s5
);
// true
System
.
out
.
println
(
s
5
==
s6
);
// true
```
```
在 Java 7 之前,String Po
ll 被放在运行时常量池中,它属于永久代。而在 Java 7,String Pol
l 被移到堆中。这是因为永久代的空间有限,在大量使用字符串的场景下会导致 OutOfMemoryError 错误。
在 Java 7 之前,String Po
ol 被放在运行时常量池中,它属于永久代。而在 Java 7,String Poo
l 被移到堆中。这是因为永久代的空间有限,在大量使用字符串的场景下会导致 OutOfMemoryError 错误。
-
[
StackOverflow : What is String interning?
](
https://stackoverflow.com/questions/10578984/what-is-string-interning
)
-
[
StackOverflow : What is String interning?
](
https://stackoverflow.com/questions/10578984/what-is-string-interning
)
-
[
深入解析 String#intern
](
https://tech.meituan.com/in_depth_understanding_string_intern.html
)
-
[
深入解析 String#intern
](
https://tech.meituan.com/in_depth_understanding_string_intern.html
)
## new String("abc")
## new String("abc")
使用这种方式一共会创建两个字符串对象(前提是 String Po
l
l 中还没有 "abc" 字符串对象)。
使用这种方式一共会创建两个字符串对象(前提是 String Po
o
l 中还没有 "abc" 字符串对象)。
-
"abc" 属于字符串字面量,因此编译时期会在 String Po
l
l 中创建一个字符串对象,指向这个 "abc" 字符串字面量;
-
"abc" 属于字符串字面量,因此编译时期会在 String Po
o
l 中创建一个字符串对象,指向这个 "abc" 字符串字面量;
-
而使用 new 的方式会在堆中创建一个字符串对象。
-
而使用 new 的方式会在堆中创建一个字符串对象。
创建一个测试类,其 main 方法中使用这种方式来创建字符串对象。
创建一个测试类,其 main 方法中使用这种方式来创建字符串对象。
...
@@ -267,7 +267,7 @@ Constant pool:
...
@@ -267,7 +267,7 @@ Constant pool:
// ...
// ...
```
```
在 Constant Po
ll 中,#19 存储这字符串字面量 "abc",#3 是 String Poll 的字符串对象,它指向 #19 这个字符串字面量。在 main 方法中,0: 行使用 new #2 在堆中创建一个字符串对象,并且使用 ldc #3 将 String Pol
l 中的字符串对象作为 String 构造函数的参数。
在 Constant Po
ol 中,#19 存储这字符串字面量 "abc",#3 是 String Pool 的字符串对象,它指向 #19 这个字符串字面量。在 main 方法中,0: 行使用 new #2 在堆中创建一个字符串对象,并且使用 ldc #3 将 String Poo
l 中的字符串对象作为 String 构造函数的参数。
以下是 String 构造函数的源码,可以看到,在将一个字符串对象作为另一个字符串对象的构造函数参数时,并不会完全复制 value 数组内容,而是都会指向同一个 value 数组。
以下是 String 构造函数的源码,可以看到,在将一个字符串对象作为另一个字符串对象的构造函数参数时,并不会完全复制 value 数组内容,而是都会指向同一个 value 数组。
...
@@ -368,10 +368,11 @@ short s1 = 1;
...
@@ -368,10 +368,11 @@ short s1 = 1;
// s1 = s1 + 1;
// s1 = s1 + 1;
```
```
但是使用 += 运算符可以执行隐式类型转换。
但是使用 +=
或者 ++
运算符可以执行隐式类型转换。
```
java
```
java
s1
+=
1
;
s1
+=
1
;
// s1++;
```
```
上面的语句相当于将 s1 + 1 的计算结果进行了向下转型:
上面的语句相当于将 s1 + 1 的计算结果进行了向下转型:
...
@@ -1042,6 +1043,10 @@ private 方法隐式地被指定为 final,如果在子类中定义的方法和
...
@@ -1042,6 +1043,10 @@ private 方法隐式地被指定为 final,如果在子类中定义的方法和
声明类不允许被继承。
声明类不允许被继承。
**4. 构造器**
声明类不允许被
`new`
实例化,多用于
`Singleton`
模式中。如果该类有子类需要继承,若该类无其他构造器,则不允许被继承。
## static
## static
**1. 静态变量**
**1. 静态变量**
...
...
notes/Java 并发.md
浏览文件 @
60ed4f49
...
@@ -740,6 +740,7 @@ java.util.concurrent(J.U.C)大大提高了并发性能,AQS 被认为是 J.
...
@@ -740,6 +740,7 @@ java.util.concurrent(J.U.C)大大提高了并发性能,AQS 被认为是 J.
```
java
```
java
public
class
CountdownLatchExample
{
public
class
CountdownLatchExample
{
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
final
int
totalThread
=
10
;
final
int
totalThread
=
10
;
CountDownLatch
countDownLatch
=
new
CountDownLatch
(
totalThread
);
CountDownLatch
countDownLatch
=
new
CountDownLatch
(
totalThread
);
...
@@ -788,6 +789,7 @@ public CyclicBarrier(int parties) {
...
@@ -788,6 +789,7 @@ public CyclicBarrier(int parties) {
```
java
```
java
public
class
CyclicBarrierExample
{
public
class
CyclicBarrierExample
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
final
int
totalThread
=
10
;
final
int
totalThread
=
10
;
CyclicBarrier
cyclicBarrier
=
new
CyclicBarrier
(
totalThread
);
CyclicBarrier
cyclicBarrier
=
new
CyclicBarrier
(
totalThread
);
...
@@ -822,6 +824,7 @@ Semaphore 类似于操作系统中的信号量,可以控制对互斥资源的
...
@@ -822,6 +824,7 @@ Semaphore 类似于操作系统中的信号量,可以控制对互斥资源的
```
java
```
java
public
class
SemaphoreExample
{
public
class
SemaphoreExample
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
final
int
clientCount
=
3
;
final
int
clientCount
=
3
;
final
int
totalRequestCount
=
10
;
final
int
totalRequestCount
=
10
;
...
@@ -866,6 +869,7 @@ FutureTask 可用于异步获取执行结果或取消执行任务的场景。当
...
@@ -866,6 +869,7 @@ FutureTask 可用于异步获取执行结果或取消执行任务的场景。当
```
java
```
java
public
class
FutureTaskExample
{
public
class
FutureTaskExample
{
public
static
void
main
(
String
[]
args
)
throws
ExecutionException
,
InterruptedException
{
public
static
void
main
(
String
[]
args
)
throws
ExecutionException
,
InterruptedException
{
FutureTask
<
Integer
>
futureTask
=
new
FutureTask
<
Integer
>(
new
Callable
<
Integer
>()
{
FutureTask
<
Integer
>
futureTask
=
new
FutureTask
<
Integer
>(
new
Callable
<
Integer
>()
{
@Override
@Override
...
@@ -971,6 +975,7 @@ produce..produce..consume..consume..produce..consume..produce..consume..produce.
...
@@ -971,6 +975,7 @@ produce..produce..consume..consume..produce..consume..produce..consume..produce.
```
java
```
java
public
class
ForkJoinExample
extends
RecursiveTask
<
Integer
>
{
public
class
ForkJoinExample
extends
RecursiveTask
<
Integer
>
{
private
final
int
threshold
=
5
;
private
final
int
threshold
=
5
;
private
int
first
;
private
int
first
;
private
int
last
;
private
int
last
;
...
...
notes/剑指 offer 题解.md
浏览文件 @
60ed4f49
...
@@ -1154,7 +1154,7 @@ public ListNode FindKthToTail(ListNode head, int k) {
...
@@ -1154,7 +1154,7 @@ public ListNode FindKthToTail(ListNode head, int k) {
## 解题思路
## 解题思路
使用双指针,一个指针 fast 每次移动两个节点,一个指针 slow 每次移动一个节点。因为存在环,所以两个指针必定相遇在环中的某个节点上。假设相遇点在下图的
z1
位置,此时 fast 移动的节点数为 x+2y+z,slow 为 x+y,由于 fast 速度比 slow 快一倍,因此 x+2y+z=2(x+y),得到 x=z。
使用双指针,一个指针 fast 每次移动两个节点,一个指针 slow 每次移动一个节点。因为存在环,所以两个指针必定相遇在环中的某个节点上。假设相遇点在下图的
y4
位置,此时 fast 移动的节点数为 x+2y+z,slow 为 x+y,由于 fast 速度比 slow 快一倍,因此 x+2y+z=2(x+y),得到 x=z。
在相遇点,slow 要到环的入口点还需要移动 z 个节点,如果让 fast 重新从头开始移动,并且速度变为每次移动一个节点,那么它到环入口点还需要移动 x 个节点。在上面已经推导出 x=z,因此 fast 和 slow 将在环入口点相遇。
在相遇点,slow 要到环的入口点还需要移动 z 个节点,如果让 fast 重新从头开始移动,并且速度变为每次移动一个节点,那么它到环入口点还需要移动 x 个节点。在上面已经推导出 x=z,因此 fast 和 slow 将在环入口点相遇。
...
...
notes/数据库系统原理.md
浏览文件 @
60ed4f49
...
@@ -294,12 +294,12 @@ SELECT ... FOR UPDATE;
...
@@ -294,12 +294,12 @@ SELECT ... FOR UPDATE;
----
----
| 隔离级别 | 脏读 | 不可重复读 | 幻影读 |
| 隔离级别 | 脏读 | 不可重复读 | 幻影读 |
加锁读 |
| :---: | :---: | :---:| :---: |
| :---: | :---: | :---:| :---: |
:---: |
| 未提交读 | √ | √ | √ |
| 未提交读 | √ | √ | √ |
× |
| 提交读 | × | √ | √ |
| 提交读 | × | √ | √ |
× |
| 可重复读 | × | × | √ |
| 可重复读 | × | × | √ |
× |
| 可串行化 | × | × | × |
| 可串行化 | × | × | × |
√ |
# 五、多版本并发控制
# 五、多版本并发控制
...
...
notes/缓存.md
浏览文件 @
60ed4f49
...
@@ -99,7 +99,7 @@ public class LRU<K, V> implements Iterable<K> {
...
@@ -99,7 +99,7 @@ public class LRU<K, V> implements Iterable<K> {
if
(
map
.
size
()
>
maxSize
)
{
if
(
map
.
size
()
>
maxSize
)
{
Node
toRemove
=
removeTail
();
Node
toRemove
=
removeTail
();
map
.
remove
(
toRemove
);
map
.
remove
(
toRemove
.
k
);
}
}
}
}
...
@@ -114,6 +114,7 @@ public class LRU<K, V> implements Iterable<K> {
...
@@ -114,6 +114,7 @@ public class LRU<K, V> implements Iterable<K> {
private
void
appendHead
(
Node
node
)
{
private
void
appendHead
(
Node
node
)
{
node
.
next
=
head
.
next
;
node
.
next
=
head
.
next
;
node
.
next
.
pre
=
node
;
node
.
pre
=
head
;
node
.
pre
=
head
;
head
.
next
=
node
;
head
.
next
=
node
;
}
}
...
@@ -122,6 +123,7 @@ public class LRU<K, V> implements Iterable<K> {
...
@@ -122,6 +123,7 @@ public class LRU<K, V> implements Iterable<K> {
private
Node
removeTail
()
{
private
Node
removeTail
()
{
Node
node
=
tail
.
pre
;
Node
node
=
tail
.
pre
;
tail
.
pre
=
node
.
pre
;
tail
.
pre
=
node
.
pre
;
node
.
pre
.
next
=
tail
;
return
node
;
return
node
;
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录