Home > other >  Snowflakes algorithm that how the project restart, the time after the callback not conflict producti
Snowflakes algorithm that how the project restart, the time after the callback not conflict producti

Time:09-24



Business code would only write four years of development, the distributed high concurrency also can't be a programmer? ->>



/* *
* snowflakes algorithm to generate uuid
*
* @ author fuce
*/
Public class SnowflakeIdWorker {

//==============================Fields===========================================
/* * start time section (2015-01-01) */
Private final long twepoch=1420041600000 l;

/* * the machine id of digits */
Private final long workerIdBits=5 l;

/* */* data identification id of the digits
Private final long datacenterIdBits=5 l;

The largest machine id/* * support, the result is 31 (the shift algorithm can quickly calculate the several binary number can the maximum number of decimal) */
Private final long maxWorkerId=1 l ^ (1 l & lt;
/* * support one of the biggest data identification id, the result is 31 */
Private final long maxDatacenterId=1 l ^ (1 l & lt;
Sequence of digits in the id/* * */
Private final long sequenceBits=12 l;

/* * machine ID left shift 12 */
Private final long workerIdShift=sequenceBits;

/* * data identification id left shift 17 (12 + 5) */
Private final long datacenterIdShift=sequenceBits + workerIdBits;

/* * time cut left shift 22 (5 + 5 + 12) */
Private final long timestampLeftShift=sequenceBits + workerIdBits + datacenterIdBits;

/* * generates sequence mask, here is 4095 (0 b111111111111 XFFF==0 4095) */
Private final long sequenceMask=1 l ^ (1 l & lt;
/* * working machine ID (0 ~ 31) */
Private long workerId;

/* * data center ID (0 ~ 31) */
Private long datacenterId;

/* * milliseconds sequence (0 ~ 4095) */
Private long sequence=0 l;

/the last time cut generated ID * * */
Private long lastTimestamp=1 l;

//==============================Constructors=====================================
/* *
* the constructor
* @ param workerId work ID (0 ~ 31)
* @ param datacenterId data center ID (0 ~ 31)
*/
Public SnowflakeIdWorker (long workerId, long datacenterId) {
If (workerId & gt; MaxWorkerId | | workerId & lt; 0 {
Throw new IllegalArgumentException (String format (" worker Id can 't be greater than % d or less than 0 ", maxWorkerId));
}
If (datacenterId & gt; MaxDatacenterId | | datacenterId & lt; 0 {
Throw new IllegalArgumentException (String format (" datacenter Id can 't be greater than % d or less than 0 ", maxDatacenterId));
}
Enclosing workerId=workerId;
Enclosing datacenterId=datacenterId;
}

//==============================the Methods==========================================
/* *
* get the next ID (this method is thread-safe)
* @ return SnowflakeId
*/
Public synchronized long nextId () {
Long timestamp=timeGen ();

//if the current time is less than the last time ID generated timestamps, shows that the system clock back before should throw an exception this time
If (timestamp & lt; LastTimestamp) {
Throw new RuntimeException (
The String. Format (" Clock version backwards. Refusing to generate the id for % d milliseconds ", lastTimestamp - timestamp));
}

//if it's the same time, in milliseconds sequence
If (lastTimestamp==timestamp) {
The sequence=(+ 1) sequence & amp; SequenceMask;
//ms sequence overflow
If (sequence==0) {
//block to the next millisecond, obtain new timestamp
Timestamp=tilNextMillis (lastTimestamp);
}
}
//timestamp change, milliseconds reset sequence
The else {
The sequence=0 l;
}

//the last time cut generated ID
LastTimestamp=timestamp;

//shift and through or operations up to 64 - bit ID
Return ((timestamp - twepoch) & lt; | (datacenterId & lt; | (workerId & lt; | sequence;
}

/* *
* block to the next millisecond, until a new timestamp
* @ param lastTimestamp generated ID last time section
* @ the return current timestamp
*/
Protected long tilNextMillis (long lastTimestamp) {
Long timestamp=timeGen ();
While (timestamp & lt;={lastTimestamp)
Timestamp=timeGen ();
}
Return the timestamp;
}

/* *
* returns the current time in milliseconds of
* @ return the current time (ms)
*/
Protected long timeGen () {
Return System. CurrentTimeMillis ();
}

//==============================Test=============================================
/* * */test
Public static void main (String [] args) {
SnowflakeIdWorker idWorker=new SnowflakeIdWorker (0, 0);
For (int I=0; I & lt; 1000; I++) {
Long id=idWorker. NextId ();
System. The out. Println (id);
}
}

/* *
* get UUID
* @ return
*/
Public static String getUUID () {
SnowflakeIdWorker idWorker=new SnowflakeIdWorker (0, 0);
Long id=idWorker. NextId ();
Return a String. The valueOf (id);
}

}

Project restart, time back, how to ensure not to repeat, at the same time to generate the id of the how to improve?

CodePudding user response:


Snowflakes algorithm made dubbo or HTTP service, as far as possible don't restart, so as to guarantee, or restart the service, before the progress of the lost, guarantee a hammer

CodePudding user response:


Heard there is a atomic classes in Java, I do not know can realize

CodePudding user response:


Come on

CodePudding user response:


Please answer

CodePudding user response:


OS level timing synchronization time
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related