webmagic icon indicating copy to clipboard operation
webmagic copied to clipboard

CountableThreadPool 的意义何在

Open MaLuxray opened this issue 2 years ago • 0 comments

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

这个方法,获取当前活跃线程数没有做并发处理,这个锁根本就没有发挥出作用

MaLuxray avatar Nov 01 '22 09:11 MaLuxray