import com.alibaba.fastjson.JSONObject; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; import java.util.function.Supplier; /** * map中添加多个执行漫的值,使用多线程的方式执行 */ public class AsynInitMapUtil { private ReentrantLock lock; private Map resultMap; private Integer timeOut; //执行超时时间 private TimeUnit unit; //执行超时时间单位 private ExecutorService executorService; private List<Supplier<Map>> supplierList; public AsynInitMapUtil(int timeOut, TimeUnit unit) { this.resultMap = new HashMap(); this.timeOut = timeOut; this.unit = unit; this.supplierList = new ArrayList<>(); this.lock = new ReentrantLock(); this.executorService = Executors.newCachedThreadPool(); } public AsynInitMapUtil(Map map, int timeOut, TimeUnit unit) { this.supplierList = new ArrayList<>(); this.lock = new ReentrantLock(); this.resultMap = map; this.timeOut = timeOut; this.unit = unit; this.executorService = Executors.newCachedThreadPool(); } public static void main(String[] args) { Map myMap = new HashMap(); myMap.put("test", true); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(simpleDateFormat.format(new Date())); new AsynInitMapUtil(myMap,5, TimeUnit.SECONDS) .addTask(() -> { Map map = new HashMap(); map.put("a1", 111); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } return map; }).addTask(() -> { Map map = new HashMap(); map.put("a2", 222); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return map; }).addTask(() -> { Map map = new HashMap(); map.put("a3", 333); return map; }).execute(); System.out.println(JSONObject.toJSONString(myMap)); System.out.println(simpleDateFormat.format(new Date())); } /** * 执行多任务 * * @return */ public AsynInitMapUtil execute() { try { CountDownLatch cd = new CountDownLatch(supplierList.size()); for (Supplier<Map> supplier : supplierList) { threadRun(() -> { try { initMap(supplier.get()); } catch (Exception e) { e.printStackTrace(); } finally { cd.countDown(); } }); } cd.await(this.timeOut, unit); } catch (InterruptedException e) { e.printStackTrace(); } finally { try { if (this.executorService != null) { this.executorService.shutdown(); } } catch (Exception e) { e.printStackTrace(); } } return this; } public AsynInitMapUtil addTask(Supplier<Map> supplier) { supplierList.add(supplier); return this; } public void initMap(Map map) { lock.lock(); try { resultMap.putAll(map); } finally { lock.unlock(); } } /** * 线程异步执行 * * @param */ public void threadRun(VoidTask voidTask) { executorService.execute(() -> { voidTask.execute(); }); } public Map getResultMap() { return resultMap; } public void setResultMap(Map resultMap) { this.resultMap = resultMap; } @FunctionalInterface private interface VoidTask { void execute(); } }
标签:map,Map,添加,new,import,多线程,public,unit From: https://www.cnblogs.com/zhiyong-666/p/18180254