为什么单线程执行器服务使用4个内核? [英] Why does a SingleThreaded Executor service use 4 cores?

查看:100
本文介绍了为什么单线程执行器服务使用4个内核?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码使用单线程执行程序服务,但是运行它使用了我计算机上的所有4个内核(每个内核平均使用率约为80%).

I have the following code which uses a single-threaded executor service, but running it uses all the 4 cores on my machine (each core around 80% usage in average).

问题是为什么会这样? 而且我对真正找到斐波那契不感兴趣!

The question is why is this happening? And I am not interested here in finding Fibonacci really!

public class MainSimpler {
    static int N=35;
    static AtomicInteger result = new AtomicInteger(0), pendingTasks = new AtomicInteger(1);
    static ExecutorService executor;

    public static void main(String[] args) {
        executor = Executors.newSingleThreadExecutor(); 
        long before = System.currentTimeMillis();
        System.out.println("Fibonacci "+N+" is ... ");
        executor.submit(new FibSimpler(N));
        waitToFinish();
        System.out.println(result.get());
        long after = System.currentTimeMillis();        
        System.out.println("Duration: " + (after - before) + " milliseconds\n");
    }

    private static void waitToFinish() {
        while (0 < pendingTasks.get()){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        executor.shutdown();
    }
}



class FibSimpler implements Runnable {
    int N;
    FibSimpler (int n) { N=n; }

    @Override
    public void run() {
        compute();
        MainSimpler.pendingTasks.decrementAndGet();
    }

    void compute() {
        int n = N;
        if (n <= 1) {
            MainSimpler.result.addAndGet(n);
            return;
        }
        MainSimpler.executor.submit(new FibSimpler(n-1));
        MainSimpler.pendingTasks.incrementAndGet();
        N = n-2;
        compute();  // similar to the F/J counterpart
    }
}

这与 mine .

推荐答案

在我的计算机上尝试过,我得到35%的CPU总使用率(4个内核).请注意,程序中至少有2个线程(主线程和执行程序线程).

Just tried on my machine and I get 35% total CPU usage (4 cores). Note that you have at least 2 threads in your program (the main thread and the executor thread).

但是,如果我将N增加到100,则由于Full GC花费了大量时间(并且我正在使用2GB的堆),CPU使用率上升了90%以上.

However, if I increase N to 100, the CPU usage goes up to 90+% because a lot of time is spent in Full GCs (and I was running with 2GB of heap).

所以看起来您的单线程变得太忙,任务开始堆积,等待执行.

So it looks like your single thread gets too busy and tasks start accumulating, waiting for being executed.

您可以尝试使用以下JVM参数运行代码:-XX:+PrintGC

You could try to run the code with the following JVM parameters: -XX:+PrintGC

我的机器上的输出如下:

The output on my machine looks like:

[GC 511999K-> 465632K(1962688K),1.0286778秒]
[GC 977632K-> 922984K(1962688K),1.199999秒]
[GC 1434984K-> 1407984K(1962688K),1.2421900秒]
[完整GC 1407984K-> 1373358K(1962688K),9.8320408秒]
[完整GC 1885358K-> 1822040K(1962688K),7.5170472秒]
[完整GC 1877375K-> 1870974K(1962688K),7.6635945秒]
[完整GC 1877374K-> 1876550K(1962688K),7.6705722秒]
[完整GC 1877374K-> 1877272K(1962688K),7.8381579秒]
[完整GC 1877372K-> 1877357K(1962688K),8.6095022秒]

[GC 511999K->465632K(1962688K), 1.0286778 secs]
[GC 977632K->922984K(1962688K), 1.1999209 secs]
[GC 1434984K->1407984K(1962688K), 1.2421900 secs]
[Full GC 1407984K->1373358K(1962688K), 9.8320408 secs]
[Full GC 1885358K->1822040K(1962688K), 7.5170472 secs]
[Full GC 1877375K->1870974K(1962688K), 7.6635945 secs]
[Full GC 1877374K->1876550K(1962688K), 7.6705722 secs]
[Full GC 1877374K->1877272K(1962688K), 7.8381579 secs]
[Full GC 1877372K->1877357K(1962688K), 8.6095022 secs]

这篇关于为什么单线程执行器服务使用4个内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆