1.1K Star 3.6K Fork 1.4K

GVP黄亿华 / webmagic

线程死锁bug

Backlog
longzhendong  Opened this issue

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()申请锁,此时,两方互相等待资源而造成死锁

total 1 participants

Comments (0)

Sign in to comment

Assignees
Labels
Not set
Projects
Milestones
Branches
Planed to start
Not set
Planed to end
Not set
Top level
Priority
Java
1
https://gitee.com/flashsword20/webmagic.git
git@gitee.com:flashsword20/webmagic.git
flashsword20
webmagic
webmagic

Search