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;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM