雪花算法单列模式实例化对象

yun-zuoyi
jiajia 12 months ago
parent ebbb1b9f34
commit 33e1c1abaf

@ -32,6 +32,26 @@ public class SnowflakeIdMaker {
private long datacenterId; private long datacenterId;
private long sequence = 0L; private long sequence = 0L;
private volatile static SnowflakeIdMaker snowflakeIdMaker = null;
/**
*
* @return
*/
public static SnowflakeIdMaker getSingleton(){
if (snowflakeIdMaker == null){
synchronized (SnowflakeIdMaker.class){
if (snowflakeIdMaker == null) {
snowflakeIdMaker = new SnowflakeIdMaker();
}
}
}
return snowflakeIdMaker;
}
/** /**
* ,ID * ,ID
*/ */
@ -42,6 +62,8 @@ public class SnowflakeIdMaker {
this.sequence = 0L; this.sequence = 0L;
} }
/** /**
* ID * ID
* @param workerId ID * @param workerId ID
@ -104,12 +126,12 @@ public class SnowflakeIdMaker {
private long lastTimestamp = -1L; private long lastTimestamp = -1L;
private long getWorkerId(){ public long getWorkerId(){
return workerId; return this.workerId;
} }
private long getDatacenterId(){ public long getDatacenterId(){
return datacenterId; return this.datacenterId;
} }
public synchronized long nextId() { public synchronized long nextId() {

Loading…
Cancel
Save