import java.util.concurrent.atomic.AtomicLong;
public class SeqUtil {
private Long lastTime = -1L;
private AtomicLong addNubmer = new AtomicLong(0);
private volatile static SeqUtil uniqueInstance = null;
public static SeqUtil getUniqueInstance() {
if (null == uniqueInstance) {
synchronized (SeqUtil.class) {
if (null == uniqueInstance) {
uniqueInstance = new SeqUtil();
}
}
}
return uniqueInstance;
}
public String getSeq() {
// 获取时间戳 取后4位
Long currentTime = System.currentTimeMillis();
String str;
if ((currentTime / 1000) == (lastTime / 1000)) {
str = addNubmer.toString();
addNubmer.addAndGet(1);
String seq = str.substring(str.length() - 4);
return seq;
} else {
lastTime = cur
并发情况下的自增序列
最新推荐文章于 2023-12-25 11:43:01 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)