简体   繁体   English

循环java实现

[英]Round robin java implementation

I was asked to do a multithreaded simulator of a specific algorithm. 我被要求做一个特定算法的多线程模拟器。 One of the tasks was to compare the regular scheduling results with round robin results. 其中一项任务是将常规计划结果与循环结果进行比较。 When I was looking for information about the round robin scheduling method I found vary general explanations and some code examples that I couldn't find any relation between them and scheduling the threads. 当我在寻找有关循环调度方法的信息时,我发现了不同的一般解释和一些代码示例,我找不到它们之间的任何关系和调度线程。 For example this code (found here on stack overflow): 例如这段代码(在堆栈溢出时找到):

public static void RR3(int numProcess, int[] cpuBurst, int[] arrivalTime){
    int quantum = 3,time = 0, temp;
    int completionTime = 0;
    LinkedList <Integer>process = new LinkedList();
    for (int i = 0; i < numProcess; i++) {
        process.add(i, cpuBurst[i]);
    }

    while (process.isEmpty() != true){

        for (int j  = 0; j < quantum; j++) {
            System.out.println(process.getFirst());
            if(process.peek() == 0 ){
                completionTime = completionTime + time;
                process.remove();
            }
            else{
                temp = process.pop();
                process.push(temp - 1);                   
                time++;                  
            }
        }
        process.addLast(process.getFirst()); 
        process.removeFirst();
    }

    double act = (double) completionTime/numProcess;
    System.out.println("-----------------RR3-----------------");
    System.out.println("             Act = " + act + "ms");  
}

I don't see anything but integers that represent the amount of process, time for each etc., but how do I actually manage their behavior? 我没有看到任何东西,只有整数代表过程的数量,每个等的时间,但我如何实际管理他们的行为? I dont see any call for a process to run or stop. 我没有看到任何要求运行或停止的进程。

You already noticed that this is an abstraction. 你已经注意到这是一个抽象。 Namely that there is no real work performed. 即没有真正的工作。 Instead, the work is just "imitated" by a set of Integers that represent the amount of work. 相反,工作只是由一组表示工作量的整数“模仿”。

The question about how to run or stop the processes is somewhat hidden in the algorithm itself: The LinkedList stores the "active" processes. 关于如何运行或停止进程的问题在算法本身中有些隐藏: LinkedList存储“活动”进程。 They are all started at the beginning. 它们都是从一开始就开始的。 In each turn, they receive a short time slot in which they can do some of their work. 在每个回合中,他们都会收到一个短时间段,他们可以在那里完成一些工作。 When all their work is done, they are removed from the list. 完成所有工作后,将从列表中删除它们。

In the simplest form, when the Integer values are replaced by real tasks, you could replace the line 在最简单的形式中,当Integer值被实际任务替换时,您可以替换该行

if(process.peek() == 0 ){ ... }

with something like 喜欢的东西

Task task = process.peek();
if (task.isFinished()) { ... }

Otherwise (in the else case), when there is work to be done, you could replace the lines 否则(在else的情况下),当有工作要做时,你可以替换行

temp = process.pop();
process.push(temp - 1);  

with something like 喜欢的东西

Task task = process.peek();
task.doALittleBitOfWork();

The code that you posted was originally part of a question , so one has to assume that there's still something wrong with it, but maybe it is sufficient to get the basic idea. 您发布的代码原本是问题的一部分,因此必须假设它仍然存在问题,但也许它足以获得基本想法。

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

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