调整分布式ID服务的时间戳获取方法

This commit is contained in:
udf_admin@163.com
2017-12-10 16:39:14 +08:00
parent 54d95210a6
commit 702ba2c8e9
2 changed files with 7 additions and 2 deletions
@@ -330,8 +330,8 @@ public class IdWorkerFactory {
*
* @return IdWorkerWrapper
*/
private IdWorkerWrapper get() {
long dwId = new Date().getTime() & ~(-1L << (IdWorkerConstant.WORKER_ID_BITS + IdWorkerConstant.DATACENTER_ID_BITS));
private synchronized IdWorkerWrapper get() {
long dwId = System.currentTimeMillis() & ~(-1L << (IdWorkerConstant.WORKER_ID_BITS + IdWorkerConstant.DATACENTER_ID_BITS));
return this.get(dwId);
}
@@ -1,6 +1,7 @@
package org.itkk.udf.id;
import lombok.extern.slf4j.Slf4j;
import org.itkk.udf.core.constant.IdWorkerConstant;
import org.junit.Test;
import java.text.SimpleDateFormat;
@@ -39,6 +40,10 @@ public class IdWorkerTest {
}
map.put(id, id);
}
long time = System.currentTimeMillis();
log.info(Long.toBinaryString(time));
long dwId = time & ~(-1L << (IdWorkerConstant.WORKER_ID_BITS + IdWorkerConstant.DATACENTER_ID_BITS));
log.info(Long.toBinaryString(dwId));
//0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000
// 1100100 0101110001 1111010110 0111000000 1 - 00001 - 00001 - 000000000000 ---> 1 ,1
// 1100100 0101110010 0110110000 1110100110 1 - 00010 - 00010 - 000000000000 ---> 2 ,2