java获取唯一时间戳Id.多线程保证唯一性

工程里有获取唯一时间戳作为id的需求,想了想用乐观锁cas实现,自旋.
cas原子性操作获得了绝对唯一的时间戳(纳秒版本).单机有效,不能分布式调用.

public class AtomicTimeStamp {

private AtomicLong timeMills = new AtomicLong(0);

private static AtomicLong at = new AtomicLong(0);

public Long getNextNaos(){
    while (true){
        long currentTimeMillis = System.nanoTime();
        long currentMill = timeMills.get();
        if(currentTimeMillis > currentMill && timeMills.compareAndSet(currentMill, currentTimeMillis)){
            return currentTimeMillis;
            //返回唯一时间戳
        }
    }
}

public static void main(String[] args) {
    AtomicTimeStamp stamp = new AtomicTimeStamp();
    ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(100, 150, 200, TimeUnit.MILLISECONDS, new PriorityBlockingQueue<>(100),new ThreadFactoryBuilder().build());
    long l = System.currentTimeMillis();
    //开启三个线程测试成功性,at自增,测试10S会有多少个成功的
    poolExecutor.execute(()->{
        while (true){
            stamp.getNextNaos();
            at.getAndIncrement();
            long l1 = System.currentTimeMillis();
            if(l1 > l + 10000 & l1<l+10010){
                System.out.println(at.longValue());
            }
        }
    });
    poolExecutor.execute(()->{
        while (true){
            stamp.getNextNaos();
            at.getAndIncrement();
            long l1 = System.currentTimeMillis();
            if (l1 > l + 10000 & l1 < l + 10010) {
                System.out.println(at.longValue());
            }
        }
    });
    poolExecutor.execute(()->{
        while (true){
            stamp.getNextNaos();
            at.getAndIncrement();
            long l1 = System.currentTimeMillis();
            if (l1 > l + 10000 & l1 < l + 10010) {
                System.out.println(at.longValue());
            }
        }
    });
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章