redis自增,然后补齐8位就行
追问redis自增是什么
追答网页链接
如果位数可以放宽点的话,可以使用uuid或者uuid-short。
我一般是用日期格式+自增id,如20180115(日期) + 0000001(自增id) 这样的格式
追问自增Id怎么来的
追答redis知道吗?如果是分布式的话,自增id可以从redis获取
如果是只有一个程序,也就是单机的话,可以直接new 一个 AtomicInteger,可以参考我的代码
public class Test implements Runnable {
private AtomicInteger count;
private CountDownLatch countDownLatch;
public Test(AtomicInteger count, CountDownLatch countDownLatch) {
this.count = count;
this.countDownLatch = countDownLatch;
}
public static void main(String[] args) throws InterruptedException {
AtomicInteger count = new AtomicInteger(0);
Long startTime = System.currentTimeMillis();
CountDownLatch countDownLatch = new CountDownLatch(100);
for (int i = 0; i < 100; i++) {
Test test = new Test(count, countDownLatch);
Thread thread = new Thread(test);
thread.start();
}
countDownLatch.await();
Long endTime = System.currentTimeMillis();
System.out.println("用时:" + (endTime - startTime));
System.out.println("count:" + count.get());
}
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
count.incrementAndGet();
}
countDownLatch.countDown();
}
}本回答被提问者采纳