When hashcode is used, the same ID will appear, although the probability is very small. Although it is not the reason for hashcode, it is another logic problem.
However, I tried to write an ID generator myself. Some IDs are int, for example, task id, and some IDs are long, for example, player ID.
First paste the code to see:
Private Static atomicinteger id = new atomicinteger (0); public static long GETID () {return (serverkit. getserverid () & 0 xFFFF) <48 | (system. currenttimemillis ()/1000l & 0 xffffffff) <16 | ID. addandget (1) & 0 xFFFF;} Private Static atomicinteger intid = new atomicinteger (0); public static int getintid () {return (INT) (system. currenttimemillis ()-interval limit L)/1000) <16l | (intid. addandget (1 ));}
View code
When generating the player ID, the server ID is also involved to facilitate later combination.
Because the server is multi-threaded, The atomicinteger type is used, and synchronization tools such as sync are no longer needed.