|
|
@ -30,35 +30,22 @@ import java.util.stream.Collectors; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class IntSnowflake { |
|
|
|
public class IntSnowflake { |
|
|
|
private static final IntSnowflake snowflake = new IntSnowflake(0L); |
|
|
|
private static final IntSnowflake snowflake = new IntSnowflake(0L); |
|
|
|
|
|
|
|
|
|
|
|
public static int next_id() { |
|
|
|
|
|
|
|
return (int) snowflake.nextId(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String next_id_str() { |
|
|
|
|
|
|
|
return snowflake.nextId() + ""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Start time cut (2020-05-03) |
|
|
|
* Start time cut (2020-05-03) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private final long twepoch = 1588435200000L; |
|
|
|
private final long twepoch = 1588435200000L; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* The number of bits occupied by workerId |
|
|
|
* The number of bits occupied by workerId |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private final int workerIdBits = 8; |
|
|
|
private final int workerIdBits = 8; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* The number of bits occupied by timestamp |
|
|
|
* The number of bits occupied by timestamp |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private final int timestampBits = 12; |
|
|
|
private final int timestampBits = 12; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* The number of bits occupied by sequence |
|
|
|
* The number of bits occupied by sequence |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private final int sequenceBits = 12; |
|
|
|
private final int sequenceBits = 12; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Maximum supported machine id, the result is 1023 |
|
|
|
* Maximum supported machine id, the result is 1023 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -82,7 +69,6 @@ public class IntSnowflake { |
|
|
|
* lowest 12 bit: sequence |
|
|
|
* lowest 12 bit: sequence |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private AtomicLong timestampAndSequence; |
|
|
|
private AtomicLong timestampAndSequence; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* instantiate an IdWorker using given workerId |
|
|
|
* instantiate an IdWorker using given workerId |
|
|
|
* |
|
|
|
* |
|
|
@ -93,6 +79,26 @@ public class IntSnowflake { |
|
|
|
initWorkerId(workerId); |
|
|
|
initWorkerId(workerId); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static int next_id() { |
|
|
|
|
|
|
|
return (int) snowflake.nextId(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String next_id_str() { |
|
|
|
|
|
|
|
return snowflake.nextId() + ""; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Map<Integer, Integer> map = new HashMap<>(); |
|
|
|
|
|
|
|
for (int i = 0; i < 100000000; i++) { |
|
|
|
|
|
|
|
long id = next_id(); |
|
|
|
|
|
|
|
map.put((int) id, (map.getOrDefault(id, 0) + 1)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
List<Integer> collect = map.values().stream().filter(item -> item > 0).collect(Collectors.toList()); |
|
|
|
|
|
|
|
System.out.println(map.size()); |
|
|
|
|
|
|
|
System.out.println(collect); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* init first timestamp and sequence immediately |
|
|
|
* init first timestamp and sequence immediately |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -200,17 +206,4 @@ public class IntSnowflake { |
|
|
|
private long generateRandomWorkerId() { |
|
|
|
private long generateRandomWorkerId() { |
|
|
|
return new Random().nextInt(maxWorkerId + 1); |
|
|
|
return new Random().nextInt(maxWorkerId + 1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Map<Integer, Integer> map = new HashMap<>(); |
|
|
|
|
|
|
|
for (int i = 0; i < 100000000; i++) { |
|
|
|
|
|
|
|
long id = next_id(); |
|
|
|
|
|
|
|
map.put((int) id, (map.getOrDefault(id, 0) + 1)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
List<Integer> collect = map.values().stream().filter(item -> item > 0).collect(Collectors.toList()); |
|
|
|
|
|
|
|
System.out.println(map.size()); |
|
|
|
|
|
|
|
System.out.println(collect); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |