简体   繁体   English

Java中的循环

[英]Round Robin in Java

I am working on a round robin algorithm in Java, and I have a loop that is not working correctly. 我正在使用Java进行循环算法,但是有一个循环无法正常工作。 I believe it is a simple logic error that I am missing. 我相信这是我所缺少的简单逻辑错误。 The exec[] array holds execution times for processes in a cpu. exec []数组保存cpu中进程的执行时间。 I need the quantam to be subtracted from that time or the amount of time left if less than the quantam. 我需要从该时间中减去量子,或者如果小于量子,则需要剩余的时间。 Then I need it to check the next process. 然后,我需要它来检查下一个过程。 Each process should have one pass through until the execution time is 0. The sum makes sure that the statements keep running while there is any one process that needs to run. 在执行时间为0之前,每个进程都应经过一次。总和确保在有任何一个进程需要运行时,语句保持运行。 The sum is simply from adding all the array element times. 该总和仅来自所有数组元素时间的总和。

while (sum != 0) {
        int show = i + 1;
        if (exec[i] != 0 && exec[i] > quant) {
            exec[i] = exec[i] - quant;
            sum = sum - quant;
            JOptionPane.showMessageDialog(null, "Process" + "  " + show + " is at" + "  " + exec[i]);
            JOptionPane.showMessageDialog(null, "sum" + " " + " is " + sum);
            if (i == irq - 1) {
                i = 0;
            } else {
                i++;
            }
        }
        if (exec[i] != 0 && exec[i] < quant) {
            exec[i] = exec[i] - exec[i];
            sum = sum - exec[i];
            JOptionPane.showMessageDialog(null, "Process" + "  " + show + " is at" + "  " + exec[i]);
            JOptionPane.showMessageDialog(null, "sum" + " " + " is " + sum);
            if (i == irq - 1) {
                i = 0;
            } else {
                i++;
            }
        }

    }

Please let me know if there is a fix or if any more information is needed. 请让我知道是否有修复程序或是否需要更多信息。 Thanks! 谢谢!

exec[i]=exec[i]-exec[i];
sum=sum-exec[i];

Is same as 与...相同

exec[i]=0;
sum=sum-0;

Also, you don't treat the case of exec[i]==quant 另外,您不对待exec [i] == quant

I am not sure, what you want to do. 我不确定您要做什么。 It sounds like "I want Multithreading to make my application faster" but within in Single-Core loop, you won't succeed. 听起来像“我希望多线程处理使我的应用程序更快”,但是在单核循环中,您将不会成功。

If you want a CPU round robin, let the JVM (or the operating system) decide. 如果要进行CPU循环,请让JVM(或操作系统)决定。 Both are made for this: 两者都是为此而设计的:

Some kind of java pseudo-Code: 某种Java伪代码:

int threadPoolSize = Runtime.getRuntime().availableProcessors();
ExecutorService pool = Executors.newFixedThreadPool(threadPoolSize);
pool.execute(new ImplementationOfCallableOrRunnable());

http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html

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

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