08 CopyOnWriteArrayList

jdk 基于 8 版本

在平时的开发中,我们经常会用到 CopyOnWriteArrayList, 利用写时复制的机制来保证并发安全, 适合多读少写的场景。

使用方式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class CopyOnWriteArrayListTest {

    @Test
    public void test() {
        List<String> data = new CopyOnWriteArrayList<>();
        data.add("1");
        assertThat(data.get(0)).isEqualTo("1");
        data.remove("1");
        assertThat(data.isEmpty()).isEqualTo(true);
    }
}

add

添加元素,写时复制

07 ThreadPoolExecutor

jdk 基于 8 版本

在平时的开发中,我们经常会用到 ThreadPoolExecutor, 需要了解源码

使用方式

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

    @Test
    void test() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                2,
                10,
                60, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(),
                new CustomizableThreadFactory("test-"),
                new ThreadPoolExecutor.CallerRunsPolicy());
        executor.submit(() -> {
            System.out.println("xxxx");
        });
        executor.shutdown();
    }
}

new

创建线程池。

Spring Security 原理

spring security 的代码比较难,之前我在 ProcessOn 上做了源码导读,所以这里只说关键点

理解关键点

  1. 认证的逻辑有多个 filter 来完成,常用的 filterUsernamePasswordAuthenticationFilterRememberMeAuthenticationFilter
  2. 认证成功,就会生成 Authentication 对象,可以从 SecurityContextHolder 获取。
  3. 有两个核心配置类,HttpSecurityWebSecurity,这两个都是用来配置 springSecurityFilterChain,只不过暴露的方法不一样

关键代码

第一步执行下面方法,添加 SecurityConfigurer。