From c48945995f379c575ef7d8aa8f0871a3260e97ca Mon Sep 17 00:00:00 2001 From: quyixiao <2621048238@qq.com> Date: Tue, 16 Sep 2025 00:58:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/heyu/api/data/utils/AsynRunDTO.java | 24 ++ .../com/heyu/api/data/utils/SanUtils.java | 232 ++++++++++++++++-- 2 files changed, 236 insertions(+), 20 deletions(-) create mode 100644 api-mapper/src/main/java/com/heyu/api/data/utils/AsynRunDTO.java diff --git a/api-mapper/src/main/java/com/heyu/api/data/utils/AsynRunDTO.java b/api-mapper/src/main/java/com/heyu/api/data/utils/AsynRunDTO.java new file mode 100644 index 0000000..3cce538 --- /dev/null +++ b/api-mapper/src/main/java/com/heyu/api/data/utils/AsynRunDTO.java @@ -0,0 +1,24 @@ +package com.heyu.api.data.utils; + +import lombok.Data; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.Future; + +@Data +public class AsynRunDTO { + + + private Map> map; + + + + public void run(){ + Map data = new HashMap<>(); + map.forEach((key, future) -> Optional.ofNullable(SanUtils.get(future)).ifPresent(osResp -> { + data.put(key, osResp); + })); + } +} diff --git a/api-mapper/src/main/java/com/heyu/api/data/utils/SanUtils.java b/api-mapper/src/main/java/com/heyu/api/data/utils/SanUtils.java index 338f57e..6c71b47 100644 --- a/api-mapper/src/main/java/com/heyu/api/data/utils/SanUtils.java +++ b/api-mapper/src/main/java/com/heyu/api/data/utils/SanUtils.java @@ -6,12 +6,14 @@ import com.heyu.api.common.test.AriseUser; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.ClassUtils; +import org.junit.Test; import java.lang.invoke.SerializedLambda; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.math.BigDecimal; import java.util.*; +import java.util.concurrent.*; @Slf4j public class SanUtils { @@ -53,10 +55,7 @@ public class SanUtils { } - - - - public static List setNull(List values, LBiFunction0 ... functions ) { + public static List setNull(List values, LBiFunction0... functions) { List list = new ArrayList<>(); try { if (CollectionUtils.isEmpty(values)) { @@ -67,8 +66,8 @@ public class SanUtils { for (LBiFunction0 function : functions) { Method method = getSetMethod(function); for (V value : values) { - Object [] objects= new Object[]{null}; - method.invoke(value,objects); + Object[] objects = new Object[]{null}; + method.invoke(value, objects); } } return list; @@ -79,8 +78,6 @@ public class SanUtils { } - - public static List list2list(List values, LBiFunction0 function) { List list = new ArrayList<>(); try { @@ -163,7 +160,6 @@ public class SanUtils { } - private static Method getSetMethod(LBiFunction0 function) { try { Method method = function.getClass().getDeclaredMethods()[1]; @@ -172,7 +168,7 @@ public class SanUtils { String methodName = serializedLambda.getImplMethodName(); String className = serializedLambda.getImplClass().replace("/", "."); - methodName = "set"+methodName.substring(3); + methodName = "set" + methodName.substring(3); Class clazz = Class.forName(className); @@ -270,7 +266,6 @@ public class SanUtils { } - /** * 基本类型包装类解析 */ @@ -288,7 +283,7 @@ public class SanUtils { return NumberUtils.objToDoubleDefault(value, null); } else if (parameterType == Byte.class || parameterType == byte.class) { return NumberUtils.objToByteDefault(value, null); - } else if (parameterType == BigDecimal.class) { + } else if (parameterType == BigDecimal.class) { return NumberUtils.objToBigDecimalDefault(value, null); } } else if (parameterType == Boolean.class || parameterType == boolean.class) { @@ -296,20 +291,20 @@ public class SanUtils { } else if (parameterType == Character.class || parameterType == char.class) { return NumberUtils.objToCharacterDefault(value, null); } else if (parameterType == String.class) { - return value !=null ? value.toString() : null ; - }else if (parameterType == Date.class && value instanceof Date){ + return value != null ? value.toString() : null; + } else if (parameterType == Date.class && value instanceof Date) { return value; } else if (parameterType == Date.class && value instanceof String) { Object target = DateUtils.parseStr2Date((String) value, YYYY_MM_DD_HH_MM_SS); if (target == null) { target = DateUtils.parseStr2Date((String) value, YYYY_MM_DD); - if(target == null){ + if (target == null) { target = DateUtils.parseStr2Date((String) value, YYYY); - if(target == null){ + if (target == null) { target = DateUtils.parseStr2Date((String) value, YYYY_MM); - if(target == null){ + if (target == null) { target = DateUtils.parseStr2Date((String) value, YYYY_MM_DD_HH); - if(target == null){ + if (target == null) { target = DateUtils.parseStr2Date((String) value, YYYY_MM_DD_HH_MM); } } @@ -357,6 +352,197 @@ public class SanUtils { return primitiveTypes.contains(clazz) ? true : false; } + + public static T get(Future t) { + + return get(t, 3000L); + + } + + + public static T get(Future t, long timeout) { + if (t != null) { + try { + return t.get(timeout, TimeUnit.MILLISECONDS); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + } + return null; + } + + + private static ExecutorService threadPool; + + + static { + threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + } + + + public static List parallel2List(List params, LBiFunction0 function) { + + List list = new ArrayList<>(); + + + Map> map = new HashMap<>(); + + for (int i = 0; i < params.size(); i++) { + V v = params.get(i); + Future countFuture = threadPool.submit(getAsynCallable(function, v)); + + + map.put(i + "", countFuture); + } + + + Map agentResult = new HashMap<>(); + + map.forEach((key, future) -> Optional.ofNullable(get(future)).ifPresent(osResp -> { + agentResult.put(key, osResp); + })); + for (Map.Entry entry : agentResult.entrySet()) { + list.add(entry.getValue()); + } + return list; + + } + + + public static Map parallel2Map(List params, LBiFunction0 function, LBiFunction0 functionKey) { + Map> map = new HashMap<>(); + + for (int i = 0; i < params.size(); i++) { + V v = params.get(i); + + Future countFuture = threadPool.submit(getAsynCallable(function, v)); + + K key = null; + + Object object = getKeyDeValue(v); + + Method method = getMethod(functionKey); + + method.setAccessible(true); + + try { + int modifiers = method.getModifiers(); + if (Modifier.isStatic(modifiers)) { + key = (K) method.invoke(null, object); + } else { + key = (K) method.invoke(object); + } + } catch (Exception e) { + e.printStackTrace(); + } + map.put(key, countFuture); + } + + Map agentResult = new HashMap<>(); + + map.forEach((key, future) -> Optional.ofNullable(get(future)).ifPresent(osResp -> { + agentResult.put(key, osResp); + })); + + return agentResult; + + } + + private static Object getKeyDeValue(Object v) { + if (v instanceof Map) { + for (Map.Entry entry : ((Map) v).entrySet()) { + return getKeyDeValue(entry.getValue()); + } + } else if (v instanceof List) { + return getKeyDeValue(((List) v).get(0)); + } else if (!isBasicDataTypes(v.getClass())) { + return v; + } + + return null; + } + + + private static Callable getAsynCallable(LBiFunction0 function0, P value) { + return new Callable() { + @Override + public R call() throws Exception { + + Object object = null; + try { + Method method = getMethod(function0); + + int modifiers = method.getModifiers(); + + if (Modifier.isStatic(modifiers)) { + + object = method.invoke(null, value); + + } else { + object = method.invoke(value); + } + + return (R) object; + } catch (Exception e) { + log.error("线程池异常",e); + } finally { + + } + return null; + } + }; + } + + + + private static Callable getAsynRun(Runnable runnable) { + return new Callable() { + @Override + public R call() throws Exception { + + Object object = null; + try { + runnable.run(); + + } catch (Exception e) { + log.error("线程池异常",e); + } + return null; + } + }; + } + + + + public static AsynRunDTO build(Runnable ... target){ + Map> map = new HashMap<>(); + for (int i = 0; i < target.length; i++) { + Runnable v = target[i]; + Future countFuture = threadPool.submit(getAsynRun(v)); + map.put(i + "", countFuture); + } + + AsynRunDTO asynRunDTO = new AsynRunDTO(); + asynRunDTO.setMap(map); + return asynRunDTO; + } + + + @Test + public void test(){ + build( + ()->{ + System.out.println("1"); + }, + ()->{ + System.out.println("2"); + } + ).run(); + } + + + + public static void main(String[] args) { AriseUser ai = new AriseUser(); ai.setName("张三"); @@ -375,9 +561,15 @@ public class SanUtils { list.add(ai2); list.add(ai3); - setNull(list, AriseUser::getAge); + Map objectMap = parallel2Map(list, SanUtils::getName, AriseUser::getAge); + System.out.println(JSON.toJSONString(objectMap)); - System.out.println(JSON.toJSONString(list)); + } + + + public static AriseUser getName(AriseUser ariseUser) { + + return ariseUser; }