数据库存储的是两个Long类型的复合主键。显示到页面的是一个27位的数字单号

 package com.yunyihenkey.common.idworker;

 /**
*
* @desc
* @author jon snow
* @version 1.0.0
*/
public class SuperSnowflakeIdWorker { public static class NextId {
private Long id1;
private Long id2; public NextId(Long id1, Long id2) {
this.id1 = id1;
this.id2 = id2;
} public Long getId1() {
return id1;
} public void setId1(Long id1) {
this.id1 = id1;
} public Long getId2() {
return id2;
} public void setId2(Long id2) {
this.id2 = id2;
} } /** 开始时间截(秒) */
public static final long birth = 1498939440L; /** 机器id所占的位数 */
public static final long workerIdBits = 20L; /** 序列在id中占的位数 */
public static final long sequenceBits = 43L; /** 支持的最大机器id (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */
public static final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** 序列最大值 */
public static final long sequenceMask = -1L ^ (-1L << sequenceBits); /** 工作机器ID(0~1048575 ) */
private long workerId; /** 秒内序列(0~8796093022207) */
private long sequence = 0L; /** 上次生成ID的时间截 */
private long lastTimestamp = -1L; public SuperSnowflakeIdWorker(long workerId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(
String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
this.workerId = workerId;
} public synchronized NextId nextId() {
long timestamp = timeGen(); // 如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常
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;
} // 上次生成ID的时间截
lastTimestamp = timestamp; // 移位并通过或运算拼到一起组成64位的ID
// long l = (workerId << sequenceBits) | sequence; // System.out.println(Long.toBinaryString(workerId) + ":+:" +
// Long.toBinaryString(sequence));
// String binaryString = Long.toBinaryString(l);
// System.out.println("生成:" + zero.substring(0, 63 - binaryString.length()) +
// binaryString);
// System.out.println("生成数字:" + l + ":原始数字:"
// + Long.valueOf(Long.toBinaryString(workerId) + Long.toBinaryString(sequence),
// 2) + "end"); // return new long[] { timestamp - birth, (workerId << sequenceBits) | sequence
// };
return new NextId(timestamp - birth, (workerId << sequenceBits) | sequence);
} /**
* 阻塞到下一个秒,直到获得新的时间戳
*
* @param lastTimestamp
* 上次生成ID的时间截
* @return 当前时间戳
*/
protected long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
} private long timeGen() {
return System.currentTimeMillis() / 1000;
} /** 测试生成id */
public static void main(String[] args) { SuperSnowflakeIdWorker idWorker = new SuperSnowflakeIdWorker(666);
long currentTimeMillis = System.currentTimeMillis();
int times = 100000000;
for (int i = 0; i < times; i++) { // 获取一个复合主键id
// NextId nextId = idWorker.nextId();
idWorker.nextId(); // System.out.println(Long.toBinaryString(id)); // System.out.println("--------------------------------");
// String zero =
// "000000000000000000000000000000000000000000000000000000000000000";// 63个0
// String binaryString = Long.toBinaryString(nextId.getId2());
// BigInteger bigInteger = new BigInteger(
// Long.toBinaryString(nextId.getId1()) + zero.substring(0, 63 -
// binaryString.length()) + binaryString,
// 2);
// String string = bigInteger.toString();
// System.out.println("数据库存储:id1=" + nextId.getId1() + "::id2=" +
// nextId.getId2() + "::::页面显示订单号:" + string);
// String string2 = bigInteger.toString(2);
// System.out.println("id的比特字节::" + string2);
//
// int endIndex = string2.length() - 63;
// System.out
// .println("前端传过来的订单号:" + string + "" + " 解析-->id1=" +
// Long.valueOf(string2.substring(0, endIndex), 2)
// + "::id2=" + Long.valueOf(string2.substring(endIndex), 2));
} long cost = System.currentTimeMillis() - currentTimeMillis;
long l = times / (cost == 0 ? 1 : cost) * 1000;
System.out.println("\r\n耗时(ms):" + cost + ",速度(万每秒):" + (l / 10000)); } // /** 测试是否重复 */
// public static final ConcurrentHashMap<Object, Object> map = new
// ConcurrentHashMap<>(1000100);
/** 测试是否重复 */
// public static void main666(String[] args) {
// // 9个线程
// for (int i = 0; i < 9; i++) {
// new Thread(new Runnable() {
// @Override
// public void run() {
// MySnow idWorker = new MySnow(Thread.currentThread().getId());
// for (int j = 0; j < 1000000; j++) {
// NextId nextId = idWorker.nextId();
// Object put = map.put(Long.toString(nextId.getId1()) +
// Long.toString(nextId.getId2()), 1);
// if (put != null) {
// System.out.println("id重复!!!");
// }
// }
// System.out.println(Thread.currentThread().getId() + ",线程跑完");
// }
// }).start();
//
// }
//
// } }
05-14 07:15
查看更多