这里我们使用时间戳生成ID,注意防止高并发情况下生成相同时间戳,我们延迟+同步锁:
package com.llpp.tool;
/**
* @Author 21326
* @Date 2024 2024/11/22 22:11
*/
public class PrimaryKey {
public synchronized Long nextId() {
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
return System.currentTimeMillis();
}
}