This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CopyOnWriteHashMap<K, V> implements Map<K, V>, Cloneable { | |
private volatile Map<K, V> internalMap = new HashMap<>(); | |
private static final Unsafe UNSAFE; | |
private static final long INTERNAL_MAP_OFFSET; | |
static { | |
try { | |
Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe"); | |
theUnsafeField.setAccessible(true); | |
UNSAFE = (Unsafe) theUnsafeField.get(null); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SnowflakeIdGenerator { | |
// 起始时间戳 (2024-01-01 00:00:00) | |
private final long START_TIMESTAMP = 1704038400000L; | |
// 每部分占用位数 | |
private final long SEQUENCE_BIT = 12; // 序列号占12位 | |
private final long MACHINE_BIT = 5; // 机器ID占5位 | |
private final long DATACENTER_BIT = 5; // 数据中心占5位 | |
// 最大值 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
你好,gist.github |