This commit is contained in:
Looly 2022-01-20 11:41:56 +08:00
parent 7c18710712
commit a6e1231085
3 changed files with 13 additions and 3 deletions

View File

@ -27,7 +27,6 @@ public class ThreadUtil {
* 1. 初始线程数为corePoolSize指定的大小
* 2. 没有最大线程数限制
* 3. 默认使用LinkedBlockingQueue默认队列大小为1024
* 4. 当运行线程大于corePoolSize放入队列队列满后抛出异常
* </pre>
*
* @param corePoolSize 同时执行的线程数大小

View File

@ -1,6 +1,7 @@
package cn.hutool.core.util;
import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.ObjectId;
import cn.hutool.core.lang.Singleton;
import cn.hutool.core.lang.Snowflake;
@ -203,8 +204,17 @@ public class IdUtil {
* @since 5.7.3
*/
public static long getDataCenterId(long maxDatacenterId) {
Assert.isTrue(maxDatacenterId > 0, "maxDatacenterId must be > 0");
if(maxDatacenterId == Long.MAX_VALUE){
maxDatacenterId -= 1;
}
long id = 1L;
final byte[] mac = NetUtil.getLocalHardwareAddress();
byte[] mac = null;
try{
mac = NetUtil.getLocalHardwareAddress();
}catch (UtilException ignore){
// ignore
}
if (null != mac) {
id = ((0x000000FF & (long) mac[mac.length - 2])
| (0x0000FF00 & (((long) mac[mac.length - 1]) << 8))) >> 6;

View File

@ -137,7 +137,8 @@ public class IdUtilTest {
@Test
public void getDataCenterIdTest(){
//按照mac地址算法拼接的算法maxDatacenterId应该是0xffffffffL>>6-1此处暂时按照0x7fffffffffffffffL-1防止最后取模溢出
final long dataCenterId = IdUtil.getDataCenterId(Long.MAX_VALUE);
Assert.assertTrue(dataCenterId > 1);
Assert.assertTrue(dataCenterId >= 0);
}
}