|  |  |  | @ -30,35 +30,22 @@ import java.util.stream.Collectors;@@ -30,35 +30,22 @@ import java.util.stream.Collectors; | 
			
		
	
		
			
				
					|  |  |  |  |  */ | 
			
		
	
		
			
				
					|  |  |  |  | public class IntSnowflake { | 
			
		
	
		
			
				
					|  |  |  |  |     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) | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     private final long twepoch = 1588435200000L; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * The number of bits occupied by workerId | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     private final int workerIdBits = 8; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * The number of bits occupied by timestamp | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     private final int timestampBits = 12; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * The number of bits occupied by sequence | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     private final int sequenceBits = 12; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * Maximum supported machine id, the result is 1023 | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
	
		
			
				
					|  |  |  | @ -82,7 +69,6 @@ public class IntSnowflake {@@ -82,7 +69,6 @@ public class IntSnowflake { | 
			
		
	
		
			
				
					|  |  |  |  |      * lowest  12 bit: sequence | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     private AtomicLong timestampAndSequence; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * instantiate an IdWorker using given workerId | 
			
		
	
		
			
				
					|  |  |  |  |      * | 
			
		
	
	
		
			
				
					|  |  |  | @ -93,6 +79,26 @@ public class IntSnowflake {@@ -93,6 +79,26 @@ public class IntSnowflake { | 
			
		
	
		
			
				
					|  |  |  |  |         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 | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
	
		
			
				
					|  |  |  | @ -200,17 +206,4 @@ public class IntSnowflake {@@ -200,17 +206,4 @@ public class IntSnowflake { | 
			
		
	
		
			
				
					|  |  |  |  |     private long generateRandomWorkerId() { | 
			
		
	
		
			
				
					|  |  |  |  |         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); | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | } |