线程在ThreadPoolExecutor中等待

时间:2016-12-16 08:40:45

标签: java threadpool

此代码运行速度非常慢。我抛弃了线程,几乎同时运行了一个线程,但是当我将ExecutorService更改为ForkJoinPool时,代码运行得非常快。我不知道为什么线程在等待,我的电脑有8个核......

enter image description here

public class Tests {

    public static void main(String[] args) throws InterruptedException {

        int NUM_OF_THREADS = 8;
        int NUM_OF_INCREMENTS = 100_000_000;
        //ExecutorService service = Executors.newWorkStealingPool();
        ExecutorService service = Executors.newFixedThreadPool(NUM_OF_THREADS);
        final Counter counter = new StupidCounter();

        long before = System.currentTimeMillis();
        for (int i = 0; i < NUM_OF_INCREMENTS; i++) {
            service.submit(newCounterClient(counter, i));
        }
        service.shutdown();
        service.awaitTermination(1, TimeUnit.MINUTES);
        long end = System.currentTimeMillis();
        System.out.println(end - before);
        System.out.println(counter.getCounter());
    }


    static class CounterClient implements Runnable {
        private Counter counter;
        private int num;

        public CounterClient(Counter counter, int num) {
            this.counter = counter;
            this.num = num;
        }

        @Override
        public void run() {
            counter.increment();
        }
    }

    static interface Counter {
        void increment();

        long getCounter();
    }

    static class StupidCounter implements Counter {
        long i = 0;

        @Override
        public void increment() {
            i++;
        }

        @Override
        public long getCounter() {
            return i;
        }
    }

}
"pool-1-thread-7" #17 prio=5 os_prio=31 tid=0x00007faaa481c000 nid=0x6503 waiting on condition [0x0000700001d6d000]
   java.lang.Thread.State: WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <0x00000006c006b3d8> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireInterruptibly(AbstractQueuedSynchronizer.java:897)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1222)
    at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:335)
    at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:439)
    at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(617Thread.java:745)

2 个答案:

答案 0 :(得分:0)

很难从此代码中得出任何真正的结论,因为它并不真正任何事情。执行程序的内部队列是瓶颈,这就是为什么你只看到1个线程&#34;工作&#34;一次。它没有真正起作用,它从队列中获取下一个任务,使所有其他线程(也正在进行下一个任务)等待。除了LinkedBlockingQueue.take()的功能外,您在此处并未真正测试任何内容。

increment()方法不是线程安全的,所以基本上你正在测试不良代码,结果几乎无关紧要。如果您使CounterClient任务执行实际工作(需要几毫秒),那么与ForkJoinPool相比,您会发现性能差异很大(如果有)。

答案 1 :(得分:0)

当使用FixedThreadPool时,大部分时间都用于GarbageCollection,因为程序会消耗很多。我尝试使用VM参数 -Xmx3g 执行,并且当VM试图避免内存不足时仍然卡在垃圾收集中。

您可能希望使用内存转储功能深入了解消费的根本原因,但我猜测它是计数器中的实例。

无论如何,根本原因似乎是使用 LinkedBlockingQueue 作为 workQueue FixedThreadPool 。由于for循环向 workQueue 添加了100_000_000个元素,因此很少有另一个Thread实现从中获取元素。所以他们大部分时间都在等待所有 CounterClient 被提交。

ForkJoinPool 使用更加非阻塞的方式,据我所知,提交的操作有多个 WorkQueue