ReentrantLock的使用

来源:互联网 发布:淘宝怎么改登录名 编辑:程序博客网 时间:2024/04/28 22:14
 //  用于重启时,保证FillAutoAllotFlowTaskLogQueueThread类中restartInit方法与run方法的执行顺序
    private static final ReentrantLock initLock = new ReentrantLock();


    /**
     * 将工作流自动处理任务线程和填充消费队列线程加入到线程池
     */
    public void autoTaskThreadExecute() {
        try {


            ExecutorService fixedThreadPool = Executors.newFixedThreadPool(THREAD_MAX_NUM);


            askLogQueueThread fillAutoQueueThread = new FlowTaskLogQueueThread();
            initLock.lock();
            try {
                fillAutoQueueThread .restartInit();
            } finally {
                initLock.unlock();
            }


            initLock.lock();
            try {
                fixedThreadPool.execute(fillAutoAllotFlowTaskLogQueueThread);
            } finally {
                initLock.unlock();
            }

        } catch (Exception e) {
            logger.info(e);
        }

    }


//  员工工号对应的锁
    private static ConcurrentHashMap<String, ReentrantLock> staffIdToLock = new ConcurrentHashMap<String, ReentrantLock>();

/**
     * 根据员工工号获取锁
     * @param staffId 工号
     * @return 该员工对应的锁
     */
    private ReentrantLock getLockByStaffId(String staffId) {
        if (!staffIdToLock.containsKey(staffId)) {
            staffIdToLock.put(staffId, new ReentrantLock());
        }
        return staffIdToLock.get(staffId);
    }

0 0
原创粉丝点击