模拟工作量固定的CPU密集型任务 [英] Simulating CPU-intensive Task having fixed amount of work

查看:131
本文介绍了模拟工作量固定的CPU密集型任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在模拟一个CPU绑定任务.每个与CPU绑定的任务计算的阶乘为800.每个与CPU绑定的任务都是由Thread执行的Runnable对象.当我增加线程数(每个线程运行一个Runnable tak)时,我发现某些线程运行得如此之快,以至于服务时间趋于零.我无法理解这种行为.代码如下.

I am simulating a CPU-Bound task. Each CPU-bound task calculate factorial of 800. Each CPU-bound task is a Runnable object executed by Thread. When i increases number of threads(Each thread runs a Runnable tak) then i found some threads runs so fast in such a way that service times tends to Zero.I could not understand this behaviour.The codes are as follows.

import java.math.BigInteger;    
public class CpuBoundJob  implements Runnable {     
    public void run() {    
         BigInteger factValue = BigInteger.ONE;
            long t1=System.nanoTime();      
            for ( int i = 2; i <= 800; i++){
              factValue = factValue.multiply(BigInteger.valueOf(i));
            }
        long t2=System.nanoTime();              
        System.out.println("Service Time(ms)="+((double)(t2-t1)/1000000));
    }    
}


public class TaskRunner extends Thread {
    CpuBoundJob job=new CpuBoundJob();
    public void run(){

    job.run();  
    }
}


public class Test {
int numberOfThreads=5;
public Test(){
    for(int i=1;i<=numberOfThreads;i++){
        TaskRunner t=new TaskRunner();
        t.start();
        }
}
public static void main(String[] args) {
    new Test(); 
    }
}

如果有5个线程,则输出如下.

In case of 5 Threads the output is as below.

Service Time(ns)=28.765821
Service Time(ns)=33.489663
Service Time(ns)=29.19727
Service Time(ns)=34.259404
Service Time(ns)=37.347448

如果有10个线程,则输出如下.

In case of 10 Threads the output is as below.

Service Time(ns)=45.647232
Service Time(ns)=3.972654
Service Time(ns)=23.494475
Service Time(ns)=12.210069
Service Time(ns)=19.382478
Service Time(ns)=15.34706
Service Time(ns)=54.769652
Service Time(ns)=20.646827
Service Time(ns)=3.28936
Service Time(ns)=29.809905
Service Time(ns)=60.798897
Service Time(ns)=50.718839
Service Time(ns)=2.727253
Service Time(ns)=2.882779
Service Time(ns)=63.864835
Service Time(ns)=42.601425
Service Time(ns)=4.029496
Service Time(ns)=4.339761
Service Time(ns)=79.396239
Service Time(ns)=2.923832
Service Time(ns)=5.773848
Service Time(ns)=3.064359
Service Time(ns)=2.446592
Service Time(ns)=2.205802
Service Time(ns)=2.212513
Service Time(ns)=2.265408
Service Time(ns)=82.51073
Service Time(ns)=2.200276
Service Time(ns)=2.289487
Service Time(ns)=2.322645
Service Time(ns)=2.201459
Service Time(ns)=2.217644
Service Time(ns)=2.197908
Service Time(ns)=2.252381
Service Time(ns)=13.564814
Service Time(ns)=2.238171
Service Time(ns)=2.199486
Service Time(ns)=2.179355
Service Time(ns)=2.237381
Service Time(ns)=2.593041
Service Time(ns)=2.444225
Service Time(ns)=2.42054
Service Time(ns)=38.745219
Service Time(ns)=81.232565
Service Time(ns)=19.612216
Service Time(ns)=22.31381
Service Time(ns)=59.521916
Service Time(ns)=59.511258
Service Time(ns)=54.439255
Service Time(ns)=11.582434

我无法理解2.4的服务时间,有时服务时间下降到0.8.为什么运行固定工作量的线程执行得这么快?

i could not understand service times of 2.4 etc and sometimes service times drop to 0.8 . why does threads running a fixed amount of work executes so fastly?

推荐答案

您如何开始这些测试?如果您每次都在冷启动,我怀疑JVM是"正在预热"和及时编译代码.在这种情况下,具有几个线程的测试将作为解释代码运行,然后编译以后的运行,甚至根据之前的运行对以后的运行进行优化. JVM就是那样的魔术.

How are you starting these tests? If you are running starting them cold each time, I suspect the JVM is "warming up" and Just In Time compiling the code. If this is the case, the tests with a few threads are running as interpreted code, and later runs are compiled, and even later runs are optimized based upon previous runs. The JVM is magic that way.

使优化不大可能实现的想法:

Ideas to make the optimizations less likely to hit:

  • 使用随机数作为工作参数(而不是循环索引)
  • 将时间用作工作参数(同上)
  • 进行一些字符串连接
  • 通过工作参数改变循环的长度

这篇关于模拟工作量固定的CPU密集型任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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