全局不重复ID生成算法,已应用在实际项目中

/** 
* 64位ID (42(毫秒)+5(机器ID)+5(业务编码)+12(重复累加)) 
* @author Polim 
*/ 
public class IdWorker { 
private final static long twepoch = 1288834974657L; 
// 机器标识位数 
private final static long workerIdBits = 5L; 
// 数据中心标识位数 
private final static long datacenterIdBits = 5L; 
// 机器ID最大值 
private final static long maxWorkerId = -1L ^ (-1L << workerIdBits); 
// 数据中心ID最大值 
private final static long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); 
// 毫秒内自增位 
private final static long sequenceBits = 12L; 
// 机器ID偏左移12位 
private final static long workerIdShift = sequenceBits; 
// 数据中心ID左移17位 
private final static long datacenterIdShift = sequenceBits + workerIdBits; 
// 时间毫秒左移22位 
private final static long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; 

private final static long sequenceMask = -1L ^ (-1L << sequenceBits); 

private static long lastTimestamp = -1L; 

private long sequence = 0L; 
private final long workerId; 
private final long datacenterId; 

public IdWorker(long workerId, long datacenterId) { 
if (workerId > maxWorkerId || workerId < 0) { 
throw new IllegalArgumentException("worker Id can't be greater than %d or less than 0"); 

if (datacenterId > maxDatacenterId || datacenterId < 0) { 
throw new IllegalArgumentException("datacenter Id can't be greater than %d or less than 0"); 

this.workerId = workerId; 
this.datacenterId = datacenterId; 


public synchronized long nextId() { 
long timestamp = timeGen(); 
if (timestamp < lastTimestamp) { 
try { 
throw new Exception("Clock moved backwards.  Refusing to generate id for "+ (lastTimestamp - timestamp) + " milliseconds"); 
} catch (Exception e) { 
e.printStackTrace(); 



if (lastTimestamp == timestamp) { 
// 当前毫秒内,则+1 
sequence = (sequence + 1) & sequenceMask; 
if (sequence == 0) { 
// 当前毫秒内计数满了,则等待下一秒 
timestamp = tilNextMillis(lastTimestamp); 

} else { 
sequence = 0; 

lastTimestamp = timestamp; 
// ID偏移组合生成最终的ID,并返回ID 
long nextId = ((timestamp - twepoch) << timestampLeftShift) 
| (datacenterId << datacenterIdShift) 
| (workerId << workerIdShift) | 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(); 



这种方式是一种比较高效的方式。也是twitter使用的一种方式。 

测试类:---------------------------------------------------------- 
import java.util.concurrent.BrokenBarrierException; 
import java.util.concurrent.CountDownLatch; 
import java.util.concurrent.CyclicBarrier; 
import java.util.concurrent.TimeUnit; 

public class IdWorkerTest { 
    public static void main(String []args){ 
        IdWorkerTest test = new IdWorkerTest(); 
        test.test2(); 
    } 

    public void test2(){ 
        final IdWorker w = new IdWorker(1,2); 
        final CyclicBarrier cdl = new CyclicBarrier(100); 

        for(int i = 0; i < 100; i++){ 
            new Thread(new Runnable() { 
                @Override 
                public void run() { 
                try { 
                    cdl.await(); 
                } catch (InterruptedException e) { 
                    e.printStackTrace(); 
                } catch (BrokenBarrierException e) { 
                    e.printStackTrace(); 
                } 
                System.out.println(w.nextId());} 
             }).start(); 
        } 
        try { 
            TimeUnit.SECONDS.sleep(5); 
        } catch (InterruptedException e) { 
           e.printStackTrace(); 
        } 

    } 
}
在MATLAB中对数据进行多项式内插并绘制曲线图是数学建模和数据可视化的重要环节。《MATLAB曲线拟合与数据内插技术详解》将为你提供深入的技术解析和实战指导。 参考资源链接:[MATLAB曲线拟合与数据内插技术详解](https://wenku.youkuaiyun.com/doc/7rinsov8av?spm=1055.2569.3001.10343) 首先,你需要确保你的数据是散点形式,且已经导入MATLAB中。然后,使用MATLAB的interp1函数进行一维多项式内插。interp1函数能够根据一组已知的散点数据,估算出这些数据点之间的未知值。 假设你有一组一维散点数据x和y,你可以通过以下代码进行二次多项式内插: ```matlab x = [1, 2, 3, 4, 5]; % 已知的散点数据 y = [1, 4, 9, 16, 25]; % 已知的散点数据对应的函数值 % 使用interp1进行二次多项式内插 xx = linspace(min(x), max(x), 100); % 生成一个更密集的x轴数据点数组 yy = interp1(x, y, xx, 'poly', 2); % 'poly', 2指定二次多项式内插 % 绘制原始散点和内插后的平滑曲线 plot(x, y, 'o', xx, yy); % 使用'o'标记原始数据点 title('多项式内插曲线图'); xlabel('X轴'); ylabel('Y轴'); ``` 上述代码首先定义了原始散点数据x和y,然后使用interp1函数进行二次多项式内插,其中'poly', 2指定了多项式的阶数。最后,使用plot函数绘制出了原始散点以及通过内插得到的平滑曲线。 通过以上步骤,你可以在MATLAB中对散点数据进行多项式内插,并绘制出平滑的曲线图。为了进一步深入理解内插技术和曲线拟合,建议详细阅读《MATLAB曲线拟合与数据内插技术详解》,该资源不仅涵盖了基础概念,还包括高级技巧和案例研究,帮助你在数值分析和工程计算中达到新的高度。 参考资源链接:[MATLAB曲线拟合与数据内插技术详解](https://wenku.youkuaiyun.com/doc/7rinsov8av?spm=1055.2569.3001.10343)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值