1.2K Star 4.8K Fork 1.9K

GVP黄亿华 / webmagic

 / 详情

线程死锁bug

待办的
创建于  
2017-12-12 23:46

public void execute(final Runnable runnable) {

    if (threadAlive.get() >= threadNum) {
        try {
            reentrantLock.lock();
            while (threadAlive.get() >= threadNum) {
                try {
                    condition.await();
                } catch (InterruptedException e) {
                }
            }
        } finally {
            reentrantLock.unlock();
        }
    }
    threadAlive.incrementAndGet();
    executorService.execute(new Runnable() {
        @Override
        public void run() {
            try {
                runnable.run();
            } finally {
                try {
                    reentrantLock.lock();
                    threadAlive.decrementAndGet();
                    condition.signal();
                } finally {
                    reentrantLock.unlock();
                }
            }
        }
    });
}

里有死锁的风险,当threadAlive数大于等于threadNum线程数,reentrantLock.lock()申请锁,循环condition.await(),与此同时 executorService.execute方法中的threadAlive的decrementAndGet也必须reentrantLock.lock()申请锁,此时,两方互相等待资源而造成死锁

评论 (0)

ZDL 创建了任务

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(1)
Java
1
https://gitee.com/flashsword20/webmagic.git
git@gitee.com:flashsword20/webmagic.git
flashsword20
webmagic
webmagic

搜索帮助

14c37bed 8189591 565d56ea 8189591