Skip to content

Instantly share code, notes, and snippets.

View wucao's full-sized avatar

吴操 wucao

View GitHub Profile
@wucao
wucao / Snowflake.java
Created September 12, 2021 15:05
雪花算法(SnowFlake) Java 实现
public class Snowflake {
/**
* configured machine id - 10 bits - gives us up to 1024 machines
*/
private static final int BITS_MACHINE_ID = 10;
/**
* sequence number - 12 bits - rolls over every 4096 per machine (with protection to avoid rollover in the same ms)
*/
@wucao
wucao / VolatileTest.java
Last active September 3, 2021 16:38
不加 volatile 导致的问题案例
public class VolatileTest {
public static boolean stop = false; // 该变量加上 volatile 后,主线程设置 stop = true 才会对 Reader 线程可见
private static class Reader extends Thread {
@Override
public void run() {
// 期望:当主线程设置 stop = true 时,Reader 线程输出 stop 并结束
// 实际:由于共享变量 stop 没有加 volatile,主线程设置 stop = true 对 Reader 线程不可见,所以 Reader 线程永远不会结束
@wucao
wucao / PropertiesToNestedMap.java
Last active September 7, 2023 08:13
Java Properties 转换为具有层级结构的 Map
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import java.util.Map;
import java.util.Properties;
public class PropertiesToNestedMap {
@wucao
wucao / OkHttpMutualTLS.java
Last active August 25, 2021 02:55
OkHttp TLS 双向认证( mutual TLS authentication )
import okhttp3.OkHttpClient;
import javax.net.ssl.*;
import java.io.IOException;
import java.io.InputStream;
import java.security.*;
import java.security.cert.CertificateException;
import java.util.Arrays;
public class OkHttpMutualTLS {