From c5c60984cb7f27d70602c50afb82c62fb7acd411 Mon Sep 17 00:00:00 2001 From: duandazhi Date: Fri, 2 Sep 2022 22:54:22 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8DavailableProcessors?= =?UTF-8?q?=EF=BC=8C=E6=BD=9C=E5=9C=A8=E7=9A=84native=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E8=8E=B7=E5=8F=96CPU=E4=B8=AA=E6=95=B0=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/core/thread/ThreadUtil.java | 4 +++- .../main/java/cn/hutool/core/util/RuntimeUtil.java | 13 ++++++++++++- .../java/cn/hutool/core/util/RuntimeUtilTest.java | 7 +++++++ .../java/cn/hutool/socket/aio/AioClientTest.java | 3 ++- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/thread/ThreadUtil.java b/hutool-core/src/main/java/cn/hutool/core/thread/ThreadUtil.java index 8a83e919f..973e6599f 100644 --- a/hutool-core/src/main/java/cn/hutool/core/thread/ThreadUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/thread/ThreadUtil.java @@ -1,5 +1,7 @@ package cn.hutool.core.thread; +import cn.hutool.core.util.RuntimeUtil; + import java.lang.Thread.UncaughtExceptionHandler; import java.util.concurrent.Callable; import java.util.concurrent.CompletionService; @@ -126,7 +128,7 @@ public class ThreadUtil { } // 最佳的线程数 = CPU可用核心数 / (1 - 阻塞系数) - int poolSize = (int) (Runtime.getRuntime().availableProcessors() / (1 - blockingCoefficient)); + int poolSize = (int) (RuntimeUtil.getProcessorCount() / (1 - blockingCoefficient)); return ExecutorBuilder.create().setCorePoolSize(poolSize).setMaxPoolSize(poolSize).setKeepAliveTime(0L).build(); } diff --git a/hutool-core/src/main/java/cn/hutool/core/util/RuntimeUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/RuntimeUtil.java index ed927a440..8e88f4ccb 100755 --- a/hutool-core/src/main/java/cn/hutool/core/util/RuntimeUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/RuntimeUtil.java @@ -13,6 +13,7 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import java.util.Stack; +import java.util.concurrent.ThreadPoolExecutor; /** * 系统运行时工具类,用于执行系统命令的工具 @@ -234,11 +235,21 @@ public class RuntimeUtil { /** * 获得JVM可用的处理器数量(一般为CPU核心数) * + *

+ * 这里做一个特殊的处理,在特殊的CPU上面,会有获取不到CPU数量的情况,所以这里做一个保护; + * 默认给一个7,真实的CPU基本都是偶数,方便区分。 + * 如果不做处理,会出现创建线程池时{@link ThreadPoolExecutor},抛出异常:{@link IllegalArgumentException} + *

+ * * @return 可用的处理器数量 * @since 5.3.0 */ public static int getProcessorCount() { - return Runtime.getRuntime().availableProcessors(); + int cpu = Runtime.getRuntime().availableProcessors(); + if (cpu <= 0) { + cpu = 7; + } + return cpu; } /** diff --git a/hutool-core/src/test/java/cn/hutool/core/util/RuntimeUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/RuntimeUtilTest.java index c7a773156..41fe208ad 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/RuntimeUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/RuntimeUtilTest.java @@ -43,4 +43,11 @@ public class RuntimeUtilTest { int pid = RuntimeUtil.getPid(); Assert.assertTrue(pid > 0); } + + @Test + public void getProcessorCountTest(){ + int cpu = RuntimeUtil.getProcessorCount(); + Console.log("cpu个数:{}", cpu); + Assert.assertTrue(cpu > 0); + } } diff --git a/hutool-socket/src/test/java/cn/hutool/socket/aio/AioClientTest.java b/hutool-socket/src/test/java/cn/hutool/socket/aio/AioClientTest.java index 1a53476d8..a3fda7606 100755 --- a/hutool-socket/src/test/java/cn/hutool/socket/aio/AioClientTest.java +++ b/hutool-socket/src/test/java/cn/hutool/socket/aio/AioClientTest.java @@ -2,6 +2,7 @@ package cn.hutool.socket.aio; import cn.hutool.core.lang.Console; import cn.hutool.core.thread.ThreadFactoryBuilder; +import cn.hutool.core.util.RuntimeUtil; import cn.hutool.core.util.StrUtil; import java.io.IOException; @@ -12,7 +13,7 @@ import java.nio.channels.AsynchronousChannelGroup; public class AioClientTest { public static void main(String[] args) throws IOException { final AsynchronousChannelGroup GROUP = AsynchronousChannelGroup.withFixedThreadPool(// - Runtime.getRuntime().availableProcessors(), // 默认线程池大小 + RuntimeUtil.getProcessorCount(), // 默认线程池大小 ThreadFactoryBuilder.create().setNamePrefix("Huool-socket-").build()// ); From d033b1ec0f078cf70ae37cc0b01a46dc94cd132c Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 5 Sep 2022 00:44:50 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=20getProcessorCount=20?= =?UTF-8?q?=E6=BD=9C=E5=9C=A8=E7=9A=84=E8=8E=B7=E5=8F=96=E4=B8=8D=E5=88=B0?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 976acb351..0bc0795bf 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.8.6.M1 (2022-09-04) +# 5.8.6.M1 (2022-09-05) ### ❌不兼容特性 * 【json 】 由于设计缺陷,导致JSONObject#write方法中Filter中key的泛型不得已变动为Object,以解决无法递归的bug(issue#I5OMSC@Gitee) @@ -24,6 +24,7 @@ * 【core 】 添加ObjectUtil的别名工具类ObjUtil * 【core 】 扩展LocalDateTimeUtil.isIn方法使用场景(pr#2589@Github) * 【core 】 MapUtil增加根据entry分组(pr#2591@Github) +* 【core 】 优化 getProcessorCount 潜在的获取不到的问题(pr#792@Gitee) * ### 🐞Bug修复 * 【http 】 修复https下可能的Patch、Get请求失效问题(issue#I3Z3DH@Gitee) From 054f3fbf612c79e42b7a37bad28b95a6f5ed3024 Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 5 Sep 2022 02:37:04 +0800 Subject: [PATCH 3/3] fix code --- .../core/convert/ConverterRegistry.java | 2 +- .../core/convert/impl/BeanConverter.java | 2 +- .../impl/TemporalAccessorConverter.java | 14 ++++++ .../cn/hutool/json/BeanConverterForJSON.java | 43 ------------------- .../main/java/cn/hutool/json/JSONUtil.java | 20 --------- .../java/cn/hutool/json/Issue2447Test.java | 2 +- 6 files changed, 17 insertions(+), 66 deletions(-) delete mode 100755 hutool-json/src/main/java/cn/hutool/json/BeanConverterForJSON.java diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java b/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java index a6903dc92..45aacb04b 100755 --- a/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java @@ -285,8 +285,8 @@ public class ConverterRegistry implements Serializable { final Class clazz = ClassLoaderUtil.loadClass("cn.hutool.json.BeanConverterForJSON"); return ((Converter)ReflectUtil.newInstance(clazz, type)).convert(value, defaultValue); }catch (final Throwable ignore){ - return new BeanConverter(type).convert(value, defaultValue); } + return new BeanConverter(type).convert(value, defaultValue); } // 无法转换 diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/impl/BeanConverter.java b/hutool-core/src/main/java/cn/hutool/core/convert/impl/BeanConverter.java index 5783b561c..9a7c23848 100644 --- a/hutool-core/src/main/java/cn/hutool/core/convert/impl/BeanConverter.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/impl/BeanConverter.java @@ -31,7 +31,7 @@ public class BeanConverter extends AbstractConverter { private final Type beanType; private final Class beanClass; - protected CopyOptions copyOptions; + private final CopyOptions copyOptions; /** * 构造,默认转换选项,注入失败的字段忽略 diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/impl/TemporalAccessorConverter.java b/hutool-core/src/main/java/cn/hutool/core/convert/impl/TemporalAccessorConverter.java index b9883b7be..fa29cabf4 100644 --- a/hutool-core/src/main/java/cn/hutool/core/convert/impl/TemporalAccessorConverter.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/impl/TemporalAccessorConverter.java @@ -1,6 +1,8 @@ package cn.hutool.core.convert.impl; import cn.hutool.core.convert.AbstractConverter; +import cn.hutool.core.convert.Convert; +import cn.hutool.core.convert.ConvertException; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; @@ -23,6 +25,7 @@ import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAccessor; import java.util.Calendar; import java.util.Date; +import java.util.Map; import java.util.Objects; /** @@ -109,6 +112,17 @@ public class TemporalAccessorConverter extends AbstractConverter map = (Map) value; + if (LocalDate.class.equals(this.targetType)) { + return LocalDate.of(Convert.toInt(map.get("year")), Convert.toInt(map.get("month")), Convert.toInt(map.get("day"))); + } else if (LocalDateTime.class.equals(this.targetType)) { + return LocalDateTime.of(Convert.toInt(map.get("year")), Convert.toInt(map.get("month")), Convert.toInt(map.get("day")), + Convert.toInt(map.get("hour")), Convert.toInt(map.get("minute")), Convert.toInt(map.get("second")), Convert.toInt(map.get("second"))); + } else if (LocalTime.class.equals(this.targetType)) { + return LocalTime.of(Convert.toInt(map.get("hour")), Convert.toInt(map.get("minute")), Convert.toInt(map.get("second")), Convert.toInt(map.get("nano"))); + } + throw new ConvertException("Unsupported type: [{}] from map: [{}]", this.targetType, map); } else { return parseFromCharSequence(convertToStr(value)); } diff --git a/hutool-json/src/main/java/cn/hutool/json/BeanConverterForJSON.java b/hutool-json/src/main/java/cn/hutool/json/BeanConverterForJSON.java deleted file mode 100755 index 969a99976..000000000 --- a/hutool-json/src/main/java/cn/hutool/json/BeanConverterForJSON.java +++ /dev/null @@ -1,43 +0,0 @@ -package cn.hutool.json; - -import cn.hutool.core.bean.BeanUtil; -import cn.hutool.core.convert.impl.BeanConverter; -import cn.hutool.json.serialize.GlobalSerializeMapping; -import cn.hutool.json.serialize.JSONDeserializer; - -import java.lang.reflect.Type; - -/** - * 针对JSON的Bean转换封装。
- * 此类时针对5.x中设计缺陷设计的类,在ConverterRegistry中通过反射调用 - * - * @param Bean类型 - * @since 5.8.6 - */ -public class BeanConverterForJSON extends BeanConverter { - - public BeanConverterForJSON(Type beanType) { - super(beanType); - } - - @Override - protected T convertInternal(final Object value) { - final Class targetType = getTargetType(); - if (value instanceof JSON) { - final JSONDeserializer deserializer = GlobalSerializeMapping.getDeserializer(targetType); - if (null != deserializer) { - //noinspection unchecked - return (T) deserializer.deserialize((JSON) value); - } - - // issue#2212@Github - // 在JSONObject转Bean时,读取JSONObject本身的配置文件 - if (value instanceof JSONGetter && BeanUtil.hasSetter(targetType)) { - final JSONConfig config = ((JSONGetter) value).getConfig(); - this.copyOptions.setIgnoreError(config.isIgnoreError()); - } - } - - return super.convertInternal(value); - } -} diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONUtil.java b/hutool-json/src/main/java/cn/hutool/json/JSONUtil.java index 027486311..f16b49bd7 100755 --- a/hutool-json/src/main/java/cn/hutool/json/JSONUtil.java +++ b/hutool-json/src/main/java/cn/hutool/json/JSONUtil.java @@ -9,12 +9,10 @@ import cn.hutool.core.util.ClassUtil; import cn.hutool.core.util.HexUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; -import cn.hutool.core.util.TypeUtil; import cn.hutool.json.serialize.GlobalSerializeMapping; import cn.hutool.json.serialize.JSONArraySerializer; import cn.hutool.json.serialize.JSONDeserializer; import cn.hutool.json.serialize.JSONObjectSerializer; -import cn.hutool.json.serialize.JSONSerializer; import java.io.File; import java.io.IOException; @@ -763,24 +761,6 @@ public class JSONUtil { return object; } - // 自定义序列化 - final JSONSerializer serializer = GlobalSerializeMapping.getSerializer(object.getClass()); - if (null != serializer) { - final Type jsonType = TypeUtil.getTypeArgument(serializer.getClass()); - if (null != jsonType) { - final JSON json; - if (serializer instanceof JSONObjectSerializer) { - json = new JSONObject(jsonConfig); - } else if (serializer instanceof JSONArraySerializer) { - json = new JSONArray(jsonConfig); - } else{ - throw new JSONException("Unsupported JSONSerializer type: " + serializer.getClass()); - } - serializer.serialize(json, object); - return json; - } - } - try { // fix issue#1399@Github if(object instanceof SQLException){ diff --git a/hutool-json/src/test/java/cn/hutool/json/Issue2447Test.java b/hutool-json/src/test/java/cn/hutool/json/Issue2447Test.java index 6452f0216..5a4509cea 100644 --- a/hutool-json/src/test/java/cn/hutool/json/Issue2447Test.java +++ b/hutool-json/src/test/java/cn/hutool/json/Issue2447Test.java @@ -12,7 +12,7 @@ public class Issue2447Test { Time time = new Time(); time.setTime(LocalDateTime.of(1970, 1, 2, 10, 0, 1, 0)); String timeStr = JSONUtil.toJsonStr(time); - Assert.assertEquals(timeStr, "{\"time\":93601000}"); + Assert.assertEquals("{\"time\":93601000}", timeStr); Assert.assertEquals(JSONUtil.toBean(timeStr, Time.class).getTime(), time.getTime()); }