在多线程处理中“预热"线程到底是什么? [英] What really is to “warm up” threads on multithreading processing?

查看:114
本文介绍了在多线程处理中“预热"线程到底是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 Java 中的多线程,正如有人向我指出的那样,我注意到线程会预热,也就是说,当它们重复执行时,它们会变得更快.我想了解为什么会发生这种情况,是否与 Java 本身有关,或者是否是每个多线程程序的常见行为.

I’m dealing with multithreading in Java and, as someone pointed out to me, I noticed that threads warm up, it is, they get faster as they are repeatedly executed. I would like to understand why this happens and if it is related to Java itself or whether it is a common behavior of every multithreaded program.

示例代码(由 Peter Lawrey 编写)如下:

The code (by Peter Lawrey) that exemplifies it is the following:

for (int i = 0; i < 20; i++) {
    ExecutorService es = Executors.newFixedThreadPool(1);
    final double[] d = new double[4 * 1024];
    Arrays.fill(d, 1);
    final double[] d2 = new double[4 * 1024];
    es.submit(new Runnable() {
    @Override
    public void run() {
        // nothing.
    }
    }).get();
    long start = System.nanoTime();
    es.submit(new Runnable() {
    @Override
    public void run() {
        synchronized (d) {
            System.arraycopy(d, 0, d2, 0, d.length);
        }
    }
    });
    es.shutdown();
    es.awaitTermination(10, TimeUnit.SECONDS);
    // get a the values in d2.
    for (double x : d2) ;
    long time = System.nanoTime() - start;
    System.out.printf("Time to pass %,d doubles to another thread and back was %,d ns.%n", d.length, time);
}

结果:

Time to pass 4,096 doubles to another thread and back was 1,098,045 ns.
Time to pass 4,096 doubles to another thread and back was 171,949 ns.
 ... deleted ...
Time to pass 4,096 doubles to another thread and back was 50,566 ns.
Time to pass 4,096 doubles to another thread and back was 49,937 ns.

即它变得更快并稳定在 50 ns 左右.这是为什么?

I.e. it gets faster and stabilises around 50 ns. Why is that?

如果我运行此代码(20 次重复),然后执行其他操作(假设对先前结果进行后处理并为另一轮多线程处理做准备),然后在同一 Runnable>ThreadPool 再重复 20 次,它就已经预热了,无论如何?

If I run this code (20 repetitions), then execute something else (lets say postprocessing of the previous results and preparation for another mulithreading round) and later execute the same Runnable on the same ThreadPool for another 20 repetitions, it will be warmed up already, in any case?

在我的程序中,我只在一个线程中执行 Runnable(实际上我拥有的每个处理核心一个,它是一个 CPU 密集型程序),然后交替执行其他一些串行处理多次.随着程序的进行,它似乎并没有变得更快.也许我可以找到一种方法来加热它......

On my program, I execute the Runnable in just one thread (actually one per processing core I have, its a CPU-intensive program), then some other serial processing alternately for many times. It doesn’t seem to get faster as the program goes. Maybe I could find a way to warm it up…

推荐答案

与 JVM 相比,线程并不是那么热.

It isn't the threads that are warming up so much as the JVM.

JVM 具有所谓的 JIT(即时)编译功能.在程序运行时,它会分析程序中发生的事情并对其进行动态优化.它通过获取 JVM 运行的字节码并将其转换为运行速度更快的本机代码来实现这一点.它可以以最适合您当前情况的方式执行此操作,因为它通过分析实际运行时行为来执行此操作.这可以(并不总是)导致很好的优化.甚至比一些在没有这些知识的情况下编译为本机代码的程序更是如此.

The JVM has what's called JIT (Just In Time) compiling. As the program is running, it analyzes what's happening in the program and optimizes it on the fly. It does this by taking the byte code that the JVM runs and converting it to native code that runs faster. It can do this in a way that is optimal for your current situation, as it does this by analyzing the actual runtime behavior. This can (not always) result in great optimization. Even more so than some programs that are compiled to native code without such knowledge.

您可以在 http://en.wikipedia.org/wiki 阅读更多内容/Just-in-time_compilation

当代码加载到 CPU 缓存中时,您可以对任何程序产生类似的效果,但我相信这将是一个较小的差异.

You could get a similar effect on any program as code is loaded into the CPU caches, but I believe this will be a smaller difference.

这篇关于在多线程处理中“预热"线程到底是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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