|
|
|
@@ -1,6 +1,5 @@ |
|
|
|
package com.iformall.common; |
|
|
|
|
|
|
|
import com.iformall.exception.MallinkException; |
|
|
|
import com.iformall.utils.IPUtil; |
|
|
|
|
|
|
|
import java.lang.management.ManagementFactory; |
|
|
|
@@ -8,13 +7,21 @@ import java.net.UnknownHostException; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
/** |
|
|
|
* 产生唯一ID的snow flake |
|
|
|
* 41(timestamp)+16(ip)+18(pid)+8(seq) |
|
|
|
* |
|
|
|
* @author changstone |
|
|
|
* |
|
|
|
*/ |
|
|
|
|
|
|
|
public class IdWorker { |
|
|
|
// ==============================Fields=========================================== |
|
|
|
/** 开始时间截 (2015-01-01) */ |
|
|
|
private final long twepoch = 1489111610226L; |
|
|
|
|
|
|
|
/** 机器id所占的位数 */ |
|
|
|
private final long workerIdBits = 5L; |
|
|
|
private final long workerIdBits = 9L; |
|
|
|
|
|
|
|
/** 数据标识id所占的位数 */ |
|
|
|
private final long dataCenterIdBits = 5L; |
|
|
|
@@ -70,8 +77,9 @@ public class IdWorker { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
String[] ipArray = ipAddr.split("\\."); |
|
|
|
Long ip = Long.valueOf(ipArray[2]) << 8 | Long.valueOf(ipArray[3]) & maxWorkerId; |
|
|
|
Long processId = Long.valueOf(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]) & maxDataCenterId; |
|
|
|
Long workerId = Long.valueOf(ipArray[3]) & maxWorkerId; /// Long.valueOf(ipArray[2]) << 8 | |
|
|
|
String pName = ManagementFactory.getRuntimeMXBean().getName(); |
|
|
|
Long dataCenterId = Long.valueOf(pName.split("@")[0]) & maxDataCenterId; |
|
|
|
|
|
|
|
if (workerId > maxWorkerId || workerId < 0) { |
|
|
|
throw new IllegalArgumentException(String.format("workerId can't be greater than %d or less than 0", maxWorkerId)); |
|
|
|
@@ -166,7 +174,7 @@ public class IdWorker { |
|
|
|
final IdWorker idWorker = IdWorker.get(); |
|
|
|
List<Thread> list = new ArrayList<>(); |
|
|
|
|
|
|
|
for(int j=0;j< 10;j++) { |
|
|
|
for(int j=0;j< 40;j++) { |
|
|
|
list.add(new Thread(() -> { |
|
|
|
long startTime = System.nanoTime(); |
|
|
|
for (int i = 0; i < 50000; i++) { |
|
|
|
|