Commit eb0af48aa8cfa7d6f07758d9f016ec27ff258962

Authored by xumiao
1 parent ce4d674c

封装同步锁,测试并发

src/main/java/com/huaheng/framework/config/ShiroConfig.java
... ... @@ -267,6 +267,7 @@ public class ShiroConfig {
267 267 filterChainDefinitionMap.put("/endpoint/**", "anon");
268 268 filterChainDefinitionMap.put("/manager/**", "anon");
269 269 filterChainDefinitionMap.put("/API/WMS/v2/**","anon");
  270 + filterChainDefinitionMap.put("/test","anon");
270 271 // 系统权限列表
271 272 // filterChainDefinitionMap.putAll(SpringUtils.getBean(IMenuService.class).selectPermsAll());
272 273  
... ...
src/main/java/com/huaheng/framework/web/controller/BaseController.java
... ... @@ -3,7 +3,10 @@ package com.huaheng.framework.web.controller;
3 3 import java.util.Date;
4 4 import java.util.List;
5 5 import java.text.SimpleDateFormat;
  6 +import java.util.Map;
  7 +import java.util.concurrent.ConcurrentHashMap;
6 8 import java.util.concurrent.Semaphore;
  9 +import java.util.concurrent.locks.ReentrantLock;
7 10  
8 11 import com.github.pagehelper.PageHelper;
9 12 import com.github.pagehelper.PageInfo;
... ... @@ -227,6 +230,37 @@ public class BaseController
227 230 return ajaxResult;
228 231 }
229 232  
  233 + Map<Object, ReentrantLock> mutexCache= new ConcurrentHashMap<>();
  234 +
  235 + public AjaxResult exec(Object key,MultiProcessListener multiProcessListener){
  236 + ReentrantLock mutex4Key = null;
  237 + ReentrantLock mutexIncache;
  238 + do {
  239 + if (mutex4Key != null) {
  240 + mutex4Key.unlock();
  241 + }
  242 + mutex4Key = mutexCache.computeIfAbsent(key, k -> new ReentrantLock());
  243 + mutex4Key.lock();
  244 + mutexIncache = mutexCache.get(key);
  245 + /**
  246 + * 执行到这里:问题有两种情况
  247 + * 1、 mutexIncache ==null,说明刚拿到锁,就被remove了
  248 + * 2、 mutexIncache !=mutex4Key,说明刚拿到锁,就被新进来的线程创建的锁覆盖了
  249 + */
  250 + }while (mutexIncache ==null|| mutexIncache !=mutex4Key);
  251 + try {
  252 + multiProcessListener.doProcess();
  253 + }catch (Exception e){
  254 + e.printStackTrace();
  255 + }finally {
  256 + if (mutex4Key.getQueueLength()==0) {
  257 + mutexCache.remove(key);
  258 + }
  259 + mutex4Key.unlock();
  260 + }
  261 + return AjaxResult.success();
  262 + }
  263 +
230 264 public interface MultiProcessListener {
231 265 AjaxResult doProcess();
232 266 }
... ...
src/main/java/com/huaheng/framework/web/controller/Test2Controller.java 0 → 100644
  1 +package com.huaheng.framework.web.controller;
  2 +
  3 +
  4 +import com.huaheng.api.wcs.domain.TaskFinishDomain;
  5 +import com.huaheng.framework.web.domain.AjaxResult;
  6 +import org.slf4j.Logger;
  7 +import org.slf4j.LoggerFactory;
  8 +import org.springframework.web.bind.annotation.PathVariable;
  9 +import org.springframework.web.bind.annotation.PostMapping;
  10 +import org.springframework.web.bind.annotation.RequestBody;
  11 +import org.springframework.web.bind.annotation.RestController;
  12 +
  13 +import java.util.concurrent.ThreadLocalRandom;
  14 +import java.util.concurrent.TimeUnit;
  15 +
  16 +/**
  17 + * 测试并发
  18 + */
  19 +@RestController
  20 +public class Test2Controller extends BaseController{
  21 + private static final Logger log= LoggerFactory.getLogger(Test2Controller.class);
  22 + @PostMapping("/test")
  23 + public void test(@RequestBody TaskFinishDomain taskFinishDomain){
  24 + exec(taskFinishDomain, new MultiProcessListener() {
  25 + @Override
  26 + public AjaxResult doProcess() {
  27 + int i = ThreadLocalRandom.current().nextInt(10000);
  28 + log.info("并发测试:[{}]:开始",taskFinishDomain+"*starting begin*&第"+i+"day!");
  29 + try {
  30 + TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextInt(5000));
  31 + } catch (InterruptedException e) {
  32 + e.printStackTrace();
  33 + }
  34 + log.info("并发测试:[{}]:结束",taskFinishDomain+"*starting begin*&第"+i+"day!");
  35 + return AjaxResult.success();
  36 + }
  37 + });
  38 +
  39 + }
  40 +}
... ...