首页>代码>bootstrap+jquery+mustache+springmvc+idworker整合demo代码>/mustache_demo/src/main/java/com/javaniu/util/IdWorker.java
                
                package com.javaniu.util;
//twitter id worker
public class IdWorker {
	private final long workerId;
	private final static long twepoch = 1288834974657L;
	private long sequence = 0L;
	private final static long workerIdBits = 4L;
	public final static long maxWorkerId = -1L ^ -1L << workerIdBits;
	private final static long sequenceBits = 10L;
	private final static long workerIdShift = sequenceBits;
	private final static long timestampLeftShift = sequenceBits + workerIdBits;
	public final static long sequenceMask = -1L ^ -1L << sequenceBits;
	private long lastTimestamp = -1L;
	public IdWorker(final long workerId) {
		super();
		if (workerId > this.maxWorkerId || workerId < 0) {
			throw new IllegalArgumentException(String.format(
					"worker Id can't be greater than %d or less than 0",
					this.maxWorkerId));
		}
		this.workerId = workerId;
	}
	public synchronized long nextId() {
		long timestamp = this.timeGen();
		if (this.lastTimestamp == timestamp) {
			this.sequence = (this.sequence + 1) & this.sequenceMask;
			if (this.sequence == 0) {
				timestamp = this.tilNextMillis(this.lastTimestamp);
			}
		} else {
			this.sequence = 0;
		}
		if (timestamp < this.lastTimestamp) {
			try {
				throw new Exception(
						String.format(
								"Clock moved backwards.  Refusing to generate id for %d milliseconds",
								this.lastTimestamp - timestamp));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		this.lastTimestamp = timestamp;
		long nextId = ((timestamp - twepoch << timestampLeftShift))
				| (this.workerId << this.workerIdShift) | (this.sequence);
		return nextId;
	}
	private long tilNextMillis(final long lastTimestamp) {
		long timestamp = this.timeGen();
		while (timestamp <= lastTimestamp) {
			timestamp = this.timeGen();
		}
		return timestamp;
	}
	private long timeGen() {
		return System.currentTimeMillis();
	}
	public static void main(String[] args) {
		IdWorker worker2 = new IdWorker(2);
		System.out.println(worker2.nextId());
	}
}
 最近下载更多
最近下载更多
                
                鸦葱     LV4
                2021年6月2日
            
            
        
                best2018     LV46
                2019年1月24日
            
            
        
                shaozhong     LV1
                2018年12月2日
            
            
        
                lxl1234     LV9
                2018年10月28日
            
            
        
                gaogaoyanjiu     LV6
                2018年9月14日
            
            
        
                a10320773     LV2
                2018年8月4日
            
            
        
                hancy666     LV33
                2018年6月21日
            
            
        
                cfy8814064     LV1
                2018年4月4日
            
            
        
                江湖危险快点跑     LV7
                2018年3月26日
            
            
        
                historyU     LV1
                2017年12月22日
            
            
         最近浏览更多
最近浏览更多
                
                win1991     LV6
                2023年5月15日
            
            
        
                srl2881552     LV10
                2022年9月6日
            
            
        
                黑     LV7
                2022年7月5日
            
            
        
                p521102212     LV1
                2022年5月14日
            
            
        
                氼乚-----------     LV5
                2022年3月18日
            
            
        
                1615651451     LV2
                2021年12月7日
            
            
        
                nihaomasb     LV7
                2021年9月4日
            
            
        
                鸦葱     LV4
                2021年6月2日
            
            
        
                秦sir3067683450     LV10
                2021年6月2日
            
            
        
                dfsfh43253hg     LV1
                2021年3月16日
            
            
        
 
     
                 
                