PMKGenerator



public class PMKGenerator {

private static long lastTime = System.currentTimeMillis();

public static long time = 0;

public static long id = 0;

public static synchronized Long getUID(){

boolean done = false;
while (!done) {

long now = System.currentTimeMillis();

if (now == lastTime) {

try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}

continue;
} else {

lastTime = now;
done = true;

String timeStr = Long.toString(lastTime);
String IdStr = timeStr;
id = Long.parseLong(IdStr);

}
}

return id;
}

public static synchronized Long getNewUID(){
return new Long(getUID());
}

}

你可能感兴趣的:(thread)