mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
7c18710712
commit
a6e1231085
@ -27,7 +27,6 @@ public class ThreadUtil {
|
||||
* 1. 初始线程数为corePoolSize指定的大小
|
||||
* 2. 没有最大线程数限制
|
||||
* 3. 默认使用LinkedBlockingQueue,默认队列大小为1024
|
||||
* 4. 当运行线程大于corePoolSize放入队列,队列满后抛出异常
|
||||
* </pre>
|
||||
*
|
||||
* @param corePoolSize 同时执行的线程数大小
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user