简体   繁体   English

在四个CPU内核/八个CPU线程上执行一个程序

[英]Executing a Single Program on Four CPU Cores/Eight CPU Threads

I would like to execute (Java 7) the following algorithm on a quadricore i7 CPU so that the latter would be 100% dedicated to executing it. 我想在四核i7 CPU上执行以下算法(Java 7),以便后者100%专用于执行它。

long n = 1000000;
int a = 5;
long sum = 0;

for (long i = 1; i <= n; i ++) {
    for (long j = 1; j <= i; j ++) {
        for (long k = 1; k <= j; k *= a) {
            sum ++;
        }
    }
}
System.out.println("Sum = " + sum);

I already took advantage of a manual solution ( java.util.concurrent.Executors , java.util.concurrent.FutureTask ...) where I would create four tasks : 我已经利用了手动解决方案( java.util.concurrent.Executorsjava.util.concurrent.FutureTask ...),在这里我将创建四个任务

  • the first one would execute within the range 1 to n/4; 第一个将在1到n / 4的范围内执行;
  • the second one would execute within the range (n/4 + 1) to n/2; 第二个将在(n / 4 + 1)到n / 2的范围内执行;
  • the third one would execute within the range (n/2 + 1) to 3n/4; 第三个将在(n / 2 + 1)到3n / 4的范围内执行;
  • the fourth one would execute within the range (3n/4 + 1) to n. 第四位将在(3n / 4 + 1)到n的范围内执行。

I was wondering if there's a straightforward/automatic solution to command the OS (I am using Windows) to dedicate "all" of the available CPU resources when this program is executed? 我想知道是否有一种直接/自动的解决方案来命令OS(我正在使用Windows)在执行该程序时专用于“所有”可用CPU资源?

I do not know if this is available under either Java or Windows. 我不知道在Java或Windows下是否可用。 In C/C++ under a UNIX-type OS, you can (with the appropriate privileges) re-prioritize your application to a negative priority level. 在UNIX类型的OS下的C / C ++中,您可以(具有适当的特权)将应用程序重新排序为负优先级。 Negative priorities are typically referred to as "real-time priorities", since the OS will not adjust them, and they will get priority over (almost) everything else on the system. 负优先级通常称为“实时优先级”,因为OS不会对其进行调整,并且它们将优先于(几乎)系统上的所有其他优先级。

I think the best you can do in Java is use the Runtime.getRuntime().availableProcessors() method to determine how many processors you have, and allocate at least that many threads. 我认为您在Java中可以做的最好的事情是使用Runtime.getRuntime()。availableProcessors()方法来确定您拥有多少个处理器,并至少分配那么多线程。

AFAIK There is no straight forward/automatic solution to command in the OS(general OS windows, linux). AFAIK在OS(通用OS窗口,Linux)中没有直接/自动的命令解决方案。 But In RTOS(Real Time OS) you can divide you'r task and dedicate "all" of the available CPU resources when you'r program is executed. 但是在RTOS(实时操作系统)中,可以在执行程序时划分任务并分配所有可用的CPU资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM