springboot处理并发接口,简单实用
public int updateTsakSuatusByTaskIdAuthorId(Integer authorId, Integer taskCode, Integer status) { TaskInfoDetailVo taskDetail = null; TaskAuthorVo taskAuthor = null; //处理并发事件 定义资源的总数量 Semaphore semaphore = new Semaphore(1); //获取可用资源数 int availablePermits = semaphore.availablePermits(); if (availablePermits > 0) { try { //请求占用一个资源 semaphore.acquire(1); taskDetail = taskSR.getTaskDetail(taskCode); taskAuthor = taskSR.getCount(authorId, taskCode); } catch (Exception e) { log.error("修改抢单状态之前查询状态判断异常", e.getMessage()); } finally { //释放一个资源 semaphore.release(1); } if (taskDetail == null) { return -1; } if (null == taskAuthor) { return -2; } if (taskDetail.getStatus() != (int) EnumTaskStatus.DCP.getKey() && taskAuthor.status != (int) EnumTaskAuthorStauts.TASK0.getKey()) { return -3; } //0作者待接受,1作者待审核,2作者已拒绝,3已选中,4被拒绝, int updateStatus = updateTaskSR.updateTsakSuatusByTaskIdAuthorId(authorId, taskCode, status, String.valueOf(new Date())); return updateStatus; } else { System.out.println("*********资源已被占用,稍后再试***********"); return 0; } }
欢迎关注微信公众号:大数据从业者