16 CountDownLatch

jdk 基于 8 版本

在平时的开发中,我们经常会用到 CountDownLatch, 它是用于线程通信的工具类。
常用使用场景就是,主线程等待子线程操作完成,然后继续执行

使用方式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class CountDownLatchTest {

    @Test
    @SneakyThrows
    void test() {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        System.out.println(Thread.currentThread() + ": 1");

        new Thread(() -> {
            System.out.println(Thread.currentThread() + ": 2");
            countDownLatch.countDown();
        }).start();

        System.out.println(Thread.currentThread() + ": 3");
        countDownLatch.await();
        System.out.println(Thread.currentThread() + ": 4");
    }
}

执行结果:

Rust Ffi

实现一个简单的 ffi 绑定, 分为C函数调用lib函数调用

C 函数调用

函数是 C 内置的,无需任何额外处理。

Rust 自定义 Macro

实现一个 json!

实现

 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
#[derive(Debug, PartialEq)]
pub enum Json {
    Null,
    Number(f64),
    Str(String),
    Array(Vec<Json>),
    Object(HashMap<String, Json>),
}

impl From<String> for Json {
    fn from(value: String) -> Self {
        Str(value)
    }
}

impl From<&str> for Json {
    fn from(value: &str) -> Self {
        Str(value.into())
    }
}

impl From<i32> for Json {
    fn from(value: i32) -> Self {
        Number(value as f64)
    }
}

#[macro_export]
macro_rules! json {
    (null) => {Json::Null};
    ([ $( $value:tt ),* ]) => {
        Json::Array(
            vec![$( json!($value) ),*]
        )
    };
    ({ $( $key:tt : $value:tt ),* }) => {
        Json::Object(
            vec![$( ($key.to_string(), json!($value)) ),*].into_iter().collect()
        )
    };
    ($value:tt) => {
        Json::from($value)
    }
}

调试方法

  • 在项目下执行命令,切换为 nightly toolchain
1
rustup override set nightly-x86_64-apple-darwin
  • main.rs 中开启 trace_macros
1
2
3
#![feature(trace_macros)]

trace_macros!(true);
  • 在编译时,控制台就会输出宏展开信息。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
note: trace_macro
  --> src/main.rs:23:13
   |
23 |     let v = json!(["1", "2", "3"]);
   |             ^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: expanding `json! { ["1", "2", "3"] }`
   = note: to `Json :: Array(vec! [json! ("1"), json! ("2"), json! ("3")])`
   = note: expanding `vec! { json! ("1"), json! ("2"), json! ("3") }`
   = note: to `< [_] > ::
           into_vec(#[rustc_box] $crate :: boxed :: Box ::
           new([json! ("1"), json! ("2"), json! ("3")]))`
   = note: expanding `json! { "1" }`
   = note: to `Json :: from("1")`
   = note: expanding `json! { "2" }`
   = note: to `Json :: from("2")`
   = note: expanding `json! { "3" }`
   = note: to `Json :: from("3")`

示例代码

github

Sso 登录流程

先介绍 OAuth2 的授权码,然后再介绍 SSO 的流程。这里的代码来自 Sa-Token

OAuth2 授权码模式

授权码模式,涉及到两个接口,获取 code获取 accessToken