springboot处理并发接口,简单实用

时间:2020-04-22
本文章向大家介绍springboot处理并发接口,简单实用,主要包括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;
    }
}

原文地址:https://www.cnblogs.com/felixzh/p/12753103.html