0%

锁的分类

可重入锁、不可重入锁

重入的意思,在获取到锁A以后,再次获取A锁。

可重入锁:synchronized, ReentrantLock, ReentrantReadWriteLock

不可重入,在获取到锁A以后,再次获取锁A之前,自己必须先释放前面加的锁。

乐观锁、悲观锁

悲观锁就是,当获取不到锁资源时,会将当前线程挂起,即进入BLOCKED或WAITING状态。

线程的挂起会涉及到用户态和内核态的切换。

用户态:JVM可以自行执行的指令。

内核态:需要操作系统才可以执行。

悲观锁:synchronized, ReentrantLock, ReentrantReadWriteLock

乐观锁:获取不到锁资源,可以再次让CPU调度,重新尝试获取锁资源。

Atomic原子类中,基于CAS乐观锁实现的。

公平锁、非公平锁

synchronized只能是非公平锁。

ReentrantLock, ReentrantReadWriteLock 可以实现公平与非公平锁。

公平锁,未获取到锁时,去排队。

非公平锁,先尝试竞争抢锁,抢不到再去排队。

互斥锁、共享锁

互斥锁:synchronized、ReentrantLock

ReentrantReadWriteLock,有互斥锁,也有共享锁。

互斥锁:同一时间点,只有一个线程持有这个锁。

共享锁:同一时间点,多个线程持有。

查看JVM相关的信息。

jinfo [option] <pid>

查看JVM使用的哪个垃圾回收器

jinfo -flags <pid>

1
2
3
4
5
6
7
8
# jinfo -flags 58256
Attaching to process ID 58256, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.212-b10
Non-default VM flags: -XX:CICompilerCount=4 -XX:InitialHeapSize=264241152 -XX:MaxHeapSize=4200595456
-XX:MaxNewSize=1399848960 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=88080384 -XX:OldSize=176160768
-XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC

可以看出是使用的 UseParallelGC

通过如下命令也可以印证。

1
2
# jinfo -flag UseParallelGC 58256
-XX:+UseParallelGC

查看类加载的信息

1
2
3
4
5
6
7
8
9
# jstat -class 58256 1000 10
Loaded Bytes Unloaded Bytes Time
14582 26192.0 1 0.9 23.92
14582 26192.0 1 0.9 23.92
14582 26192.0 1 0.9 23.92
14582 26192.0 1 0.9 23.92
14582 26192.0 1 0.9 23.92
14582 26192.0 1 0.9 23.92
14582 26192.0 1 0.9 23.92

查看垃圾回收器的信息

1
2
3
4
5
6
7
8
# jstat -gc 58256 1000 10
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
28160.0 10240.0 0.0 9808.1 597504.0 430347.2 251904.0 69700.9 78612.0 74904.0 10300.0 9577.3 17 0.643 3 1.166 1.809
28160.0 10240.0 0.0 9808.1 597504.0 430347.2 251904.0 69700.9 78612.0 74904.0 10300.0 9577.3 17 0.643 3 1.166 1.809
28160.0 10240.0 0.0 9808.1 597504.0 430347.2 251904.0 69700.9 78612.0 74904.0 10300.0 9577.3 17 0.643 3 1.166 1.809
28160.0 10240.0 0.0 9808.1 597504.0 430347.2 251904.0 69700.9 78612.0 74904.0 10300.0 9577.3 17 0.643 3 1.166 1.809
28160.0 10240.0 0.0 9808.1 597504.0 430347.2 251904.0 69700.9 78612.0 74904.0 10300.0 9577.3 17 0.643 3 1.166 1.809
28160.0 10240.0 0.0 9808.1 597504.0 430347.2 251904.0 69700.9 78612.0 74904.0 10300.0 9577.3 17 0.643 3 1.166 1.809

查看是否有死锁

1
# jstack 58256

查看堆内存数据

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
# jmap -heap 58256
Attaching to process ID 58256, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.212-b10

using thread-local object allocation.
Parallel GC with 8 thread(s)

Heap Configuration:
MinHeapFreeRatio = 0
MaxHeapFreeRatio = 100
MaxHeapSize = 4200595456 (4006.0MB)
NewSize = 88080384 (84.0MB)
MaxNewSize = 1399848960 (1335.0MB)
OldSize = 176160768 (168.0MB)
NewRatio = 2
SurvivorRatio = 8
MetaspaceSize = 21807104 (20.796875MB)
CompressedClassSpaceSize = 1073741824 (1024.0MB)
MaxMetaspaceSize = 17592186044415 MB
G1HeapRegionSize = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
capacity = 589299712 (562.0MB)
used = 93162144 (88.84634399414062MB)
free = 496137568 (473.1536560058594MB)
15.808958006074844% used
From Space:
capacity = 10485760 (10.0MB)
used = 10174544 (9.703201293945312MB)
free = 311216 (0.2967987060546875MB)
97.03201293945312% used
To Space:
capacity = 30408704 (29.0MB)
used = 0 (0.0MB)
free = 30408704 (29.0MB)
0.0% used
PS Old Generation
capacity = 257949696 (246.0MB)
used = 71381888 (68.0750732421875MB)
free = 186567808 (177.9249267578125MB)
27.672794000889226% used

29817 interned Strings occupying 3078576 bytes.

返回类

1
2
3
4
5
6
7
8
9
List<StudentCourses> queryStudentCourses();

package xxx.StudentCourses

@Data
public class StudentCourses {
private String name;
private List<String> courses;
}
1
2
3
4
5
6
<resultMap id="resultMap" type="xxx.StudentCourses">
<result column="name" property="name" />
<collection property="courses" ofType="string">
<result column="course" />
</collection>
</resultMap>
1
2
3
4
<select id="queryStudentCourses" resultMap="">
SELECT s.name, sc.course FROM student_course sc
JOIN student s ON(s.id = sc.student_id)
</select>