1、UML时序图
在画时序图的时候
我们的思路应该是这样的:
从左到右(创建好节点),然后,从上到下(编写节点间的交互逻辑),写代码画图。
案例1(volatile修饰的变量在多线程间的工作流程):
sequenceDiagram
participant t1 as t1 线程
participant num as num = 0
participant ready as volatile ready = false
participant t2 as t2 线程
t1 -->> t1:num=2
t1 ->> ready:ready=true
Note over t1,ready:写屏障
Note over num,t2:读屏障
t2 ->> ready:读取ready=true
t2->> num:读取num=2
效果如下:
案例2(JVM字节码在多线程中发生的指令重排):
sequenceDiagram
participant t1 as t1
participant INSTANCE AS INSTANCE
participant t2 as t2
t1 ->> t1:17:new
t1 ->> t1:20:dup
t1->> INSTANCE:24:putstatic(给 INSTANCE 赋值)
INSTANCE ->> t2:0:getstatic(获取 INSTANCE 引用)
t2->>t2:3:ifnonnull 37(判断不为空,跳转到 37 行)
t2 ->> INSTANCE:37:getstatic(获取 INSTANCE 引用)
t2->>t2:40:areturn(返回)
t2 ->> t2 :使用对象
t1 ->> t1:21:invokespecial(调用构造方法)
效果如下:
2、流程图(纵向和横向)
在流程图的时候
LR 指左边到右边,所以是横向流程图
TD 指上边到下边,所以是纵向流程图
我们的思路应该是这样的:
先定义好所有的流程节点,再确定好各个流程节点的指向关系,最后补充流程线上的条件
案例1(多线程处理垃圾袋):
graph TD
s(保洁阿姨)
m(主人)
g1(垃圾袋)
g2(新垃圾袋)
s-. 倒空 .-> g1
m -- 检查 --> g1
g1 -- 已满 --> g2
g1 -- 还空 --> g1
效果如下:
案例2(LongAdder类中的add方法原理图):
graph LR
A(当前线程) --> B(Cells)
B --> |为空| C(cas base 累加)
B --> |不为空| D(当前线程 cell 是否创建)
C --> |成功| E(return)
C --> |失败| F(longAccumulate)
D --> |已创建| G(cas cell 累加)
D --> |没创建| F(longAccumulate)
G --> |成功| E(return)
G --> |失败| F(longAccumulate)
另一种写法(推荐):
graph LR
A(当前线程)
B(Cells)
C(cas base 累加)
D(当前线程 cell 是否创建)
E(return)
F(longAccumulate)
G(cas cell 累加)
A --> B
B -- 为空 --> C
B -- 不为空 --> D
C -- 成功 --> E
C -- 失败 --> F
D -- 已创建 --> G
D -- 没创建 --> F
G -- 成功 --> E
G -- 失败 --> F
3、数学公式
案例1(对数与指数):
$$
x 20^x=2020\\101^y=2020\\求:\frac{1}{x}+\frac{1}{y}=?\\
由:20^x=2020 \quad 得 \quad x=\log_{20}2020\\
\qquad 101^y=2020 \quad 得 \quad y=\log_{101}2020\\
所以\qquad \frac{1}{x}+\frac{1}{y} = \frac{1}{\log_{20}2020}+\frac{1}{\log_{101}2020}\\
\quad\\
=\log_{2020}20+\log_{2020}101\\
\quad\\
=\log_{2020}(20 * 101)\\
\quad\\
=\log_{2020}2020\\
\quad\\
=1
$$
参考:
字体行距等基本设置
http://www.95408.com/blog/3283.html
Typora画图案例:
https://blog.csdn.net/zyxhangiian123456789/article/details/102479437
https://www.runoob.com/markdown/md-advance.html
https://www.jianshu.com/p/7ddbb7dc8fec
Typora写数学公式教程及案例:
https://blog.csdn.net/happyday_d/article/details/83715440
https://www.cnblogs.com/YanQing1998/p/10986911.html
https://blog.csdn.net/luolang_103/article/details/81289529 空格和换行
把案例都写一遍基本就掌握它的语法了
没什么原理需要探究,就是多练习,记忆语法,提升熟练度
很简单