在Java并发编程中,高效地生成唯一序号是一个常见的需求,尤其是在高并发场景下。正确的实现方式可以显著提高系统的性能和稳定性,避免锁冲突和资源竞争。本文将详细介绍Java中高效取序号的技巧,帮助开发者告别锁冲突,轻松应对高并发场景。
1. 引言
在高并发环境下,生成唯一序号通常需要考虑以下问题:
- 原子性:确保生成的序号是唯一的,不被其他线程干扰。
- 高效性:减少锁的使用,提高并发性能。
- 可扩展性:随着系统规模的扩大,序号生成策略应能保持高效。
2. 传统序列号生成方法
2.1 使用锁
最简单的方法是使用synchronized关键字或ReentrantLock来保证线程安全。例如:
public class SequenceNumberGenerator {
private int number = 0;
public synchronized int getNextNumber() {
return number++;
}
}
这种方法简单易用,但缺点是效率低下,在高并发场景下会导致严重的性能瓶颈。
2.2 使用原子类
Java提供了AtomicInteger等原子类,可以保证操作的原子性。例如:
import java.util.concurrent.atomic.AtomicInteger;
public class SequenceNumberGenerator {
private AtomicInteger number = new AtomicInteger(0);
public int getNextNumber() {
return number.incrementAndGet();
}
}
这种方法比使用锁更高效,但仍然存在线程竞争。
3. 高效序列号生成技巧
3.1 使用Snowflake算法
Snowflake算法是由Twitter开源的分布式唯一ID生成算法,可以生成64位的长整型ID。该算法将时间戳、数据中心ID、机器ID和序列号组合在一起,确保了ID的唯一性。
public class SnowflakeIdGenerator {
private long workerId;
private long datacenterId;
private long sequence = 0L;
private long twepoch = 1288834974657L;
private long workerIdBits = 5L;
private long datacenterIdBits = 5L;
private long maxWorkerId = -1L ^ (-1L << workerIdBits);
private long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
private long sequenceBits = 12L;
private long workerIdShift = sequenceBits;
private long datacenterIdShift = sequenceBits + workerIdBits;
private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
private long sequenceMask = -1L ^ (-1L << sequenceBits);
private long lastTimestamp = -1L;
public SnowflakeIdGenerator(long workerId, long datacenterId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
public synchronized long nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp);
}
} else {
sequence = 0L;
}
lastTimestamp = timestamp;
return ((timestamp - twepoch) << timestampLeftShift) | (datacenterId << datacenterIdShift) | (workerId << workerIdShift) | sequence;
}
private long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
}
private long timeGen() {
return System.currentTimeMillis();
}
}
3.2 使用TwitterId算法
TwitterId算法与Snowflake算法类似,也是基于时间戳、数据中心ID和机器ID生成唯一ID。
public class TwitterIdGenerator {
private long workerId;
private long datacenterId;
private long sequence = 0L;
private long twepoch = 1288834974657L;
private long workerIdBits = 5L;
private long datacenterIdBits = 5L;
private long maxWorkerId = -1L ^ (-1L << workerIdBits);
private long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
private long sequenceBits = 12L;
private long workerIdShift = sequenceBits;
private long datacenterIdShift = sequenceBits + workerIdBits;
private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
private long sequenceMask = -1L ^ (-1L << sequenceBits);
private long lastTimestamp = -1L;
public TwitterIdGenerator(long workerId, long datacenterId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
public synchronized long nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp);
}
} else {
sequence = 0L;
}
lastTimestamp = timestamp;
return ((timestamp - twepoch) << timestampLeftShift) | (datacenterId << datacenterIdShift) | (workerId << workerIdShift) | sequence;
}
private long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
}
private long timeGen() {
return System.currentTimeMillis();
}
}
4. 总结
本文介绍了Java中高效取序号的技巧,包括使用锁、原子类、Snowflake算法和TwitterId算法。在实际应用中,应根据具体场景选择合适的算法,以提高系统的性能和稳定性。
