Build Java code Framework
Introducing auxiliary classes and base classes
Pkgen for PK Generator
Java code
Package net.sw.util;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.concurrent.locks.Lock;
Import Java.util.concurrent.locks.ReentrantLock;
/** * * Used to generate a unique PK * The PK generator generates a 19-bit unique Long value that is accurate to one out of 10,000 seconds, * the last one is the mantissa, which is used to guarantee the uniqueness of PK in a cluster environment.
* * This PK generation although depends on the time, but in the running process change time will not affect PK, * but if the system time after the adjustment, and then reboot, it may cause PK duplication. * * The PK generator is not dependent on the database, and is faster than the PK generator that relies on the data table, and the value is readable * * @author <a href= "mailto:yang_y@sysnet.com.cn" >young yang</a&
Gt
* * public class Pkgen {/** * PK generation lock, used to limit the same moment only one thread into the PK generation calculation * * Private final lock lock = new Reentrantlock (); /** * The number of milliseconds to initialize, because that time changes with the system time, * So the calculation method is that time plus the difference calculated by Nanotime/private final static Long Startmilli = Sys
Tem.currenttimemillis ();
/** * Initialization of the nanosecond number, used to measure the time difference, Nanotime will not change with the system changes and changed the final static long Startnano = System.nanotime ();
/** * Record the last generated PK, if the newly generated PK and last equivalent, you need to generate * * private volatile long lastpk =-1; Private final static SiMpledateformat DateFormat = new SimpleDateFormat (//"yyyymmddhhmmsssss");
Private final static SimpleDateFormat DateFormat = new SimpleDateFormat ("Yymmddhhmmsssss");
/** * Returns the mantissa of long PK, the mantissa can be used in the cluster environment to determine the PK by which node generated * mantissa through configuration set * * private int suffix = 0;
Private static final Map<integer, pkgen> instancemap = new Hashmap<integer, pkgen> ();
The correct parameters must be provided to ensure the uniqueness of the suffix in the cluster environment pkgen (int suffix) {this.suffix = suffix;
Synchronized static Pkgen getinstance () {return getinstance (0);
/** * In a stand-alone environment, it should be guaranteed to use the same suffix * in a clustered environment, different machines must provide different suffix to guarantee the uniqueness of the generated ID * @param suffix * Unique logo * *
Public synchronized static Pkgen getinstance (int suffix) {Pkgen pkgen;
if (!instancemap.containskey (suffix)) {Pkgen = new Pkgen (suffix);
Instancemap.put (suffix, pkgen);
else {Pkgen = instancemap.get (suffix);
return Pkgen; /** * Returns the next long PK, format:2006111423361344491 <br> * yyyymmddhhmmsssss + MacroSeconds + suffix * @return long PK */public long NEXTPK () {lock.lock ();
try {long newpk; do {Long Pastnano = System.nanotime ()-Startnano;/nanosecond time difference long millitime = pastnano/1000000;//Get millisecond differential lon G Macrotime = (pastnano/100000)% 10; Get microsecond First,//calculated Long PK, precision to one out of 10,000 seconds (hundred subtle), plus the mantissa, a total of 19 bits, this is the largest number of long.max_value NEWPK = Long.parselong (dateformat.for
Mat (New Date (Startmilli + millitime)) + macrotime + suffix); while (LASTPK = = NEWPK); If the generated is the same, the LASTPK = NEWPK is computed again;
Set LASTPK return NEWPK;
finally {Lock.unlock ();
} public static void Main (string[] args) throws Exception {Pkgen pkg = pkgen.getinstance (0);
int i = 0;
Long now = System.currenttimemillis ();
while (i++ <) {System.out.println (PKG.NEXTPK ());
} System.out.println ("Time:" + (System.currenttimemillis ()-now);
}
}