jeudi 21 mars 2019

JAVA: Why is eclipse calling my second else-if statement a "dead code"?

I am writing a code in JAVA on Eclipse IDE, and I am having a problem with my if-else statement. From my third outer "if", I have two "else-if" blocks, and Eclipse is calling my second "else-if" a dead code and skips it -- I checked it using Eclipse debugger, but I don't know why. Eclipse keeps suggesting me to delete the second else-if statement, which significantly changes my code.

Please let me know if there is any other information that I can add, and thanks in advance for all the help!

Here is the code:

public static void eachCycleUni (Queue processes, int numProcesses, Process[] allProcesses, Process[] original) {   
    Queue<Process> readyProcesses = new LinkedList<Process>();      //Stores processes that are ready
    //For summary
    int currNumProcesses = numProcesses; //3
    int terminatedProcesses = 0;
    int cycleNumber = 0;            //for finishing time
    int totalTurnaroundTime = 0;    //for avg. turnaround time
    int totalRunningTime = 0;       //for cPU utilization
    int totalBlockedTime = 0;       //for I/O utilization
    int totalWaitingTime = 0;       //for avg. waiting time

    String beforeEachCycle = "Before Cycle ";
    File fileInput = new File("random-numbers.txt");
    Scanner randomInput = null;
    try {
        randomInput = new Scanner(fileInput);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Process currentRunning = null;
    Process currentBlocked = null;
    int CPUrandom = 0;
    int IOrandom = 0;
    for (int p = 0; p < allProcesses.length; p++) {
        Process currentProcess = allProcesses[p];
        while (terminatedProcesses != numProcesses) {
            if (currentBlocked != null) {
                if (currentBlocked.IOcycle + 1 == cycleNumber) {
                    System.out.println("Find I/O burst when blocking a process " + IOrandom);
                }
            }
            if (currentRunning != null) {
                if (currentRunning.CPUcycle + 1 == cycleNumber) {
                    System.out.println("Find burst when choosing ready process to run " + CPUrandom);
                }
            }
            System.out.printf("%s %d: ", beforeEachCycle, cycleNumber);
            for (int j = 0; j < allProcesses.length; j++) {
                Process thisProcess = allProcesses[j];
                if (thisProcess == currentRunning) {
                    System.out.printf("   %s: %d", thisProcess.state, thisProcess.CPUburstRemaining);
                }
                else if (thisProcess.state == "blocked") {
                    System.out.printf("   %s: %d", thisProcess.state, thisProcess.IOburstRemaining);
                }
                else {
                    System.out.printf("   %s: %d", thisProcess.state, 0);
                }
            }
            System.out.printf(".\n");

            //Nothing --> Nothing or Nothing --> currentRunning
            if (currentRunning == null && currentBlocked == null) {
                if (currentProcess.a > cycleNumber) { //unstarted and unarrived
                    //Do nothing?
                }
                else if (currentProcess.a <= cycleNumber && currentProcess.IOburstRemaining == 0) { //unstarted but arrived and can run
                    currentRunning = currentProcess;
                    currentRunning.state = "running";
                    CPUrandom = randomInput.nextInt();
                    currentRunning.CPUburstRemaining = randomOS(CPUrandom, currentRunning.b);
                    if (currentRunning.c < currentRunning.CPUburstRemaining) {
                        currentRunning.CPUburstRemaining = currentRunning.c;
                    } 
                    currentRunning.CPUcycle = cycleNumber;
                    int id = currentRunning.id; //updated currentRunning
                    allProcesses[id] = currentRunning;
                }
            }
            //currentRunning --> --, or currentRunning --> currentBlocked, or currentRunning --> terminated
            else if (currentRunning.CPUburstRemaining != 0 && currentBlocked == null) {
                currentRunning.CPUburstRemaining--;
                totalRunningTime++;
                currentRunning.c--;
                currentRunning.finishingTime++;
                int id = currentRunning.id; //updated currentRunning
                allProcesses[id] = currentRunning;
                if (currentRunning.CPUburstRemaining == 0) {
                    currentBlocked = currentRunning;
                    currentRunning = null;
                    if (currentBlocked.c == 0) {
                        currentBlocked.state = "terminated";
                        id = currentBlocked.id; //updated currentBlocked (terminated)
                        allProcesses[id] = currentBlocked;
                        currentBlocked = null;
                        terminatedProcesses++;
                    }
                    else {
                        currentBlocked.state = "blocked";
                        IOrandom = randomInput.nextInt();
                        currentBlocked.IOburstRemaining = randomOS(IOrandom, currentBlocked.io);
                        //processes.add(currentBlocked);
                        currentBlocked.IOcycle = cycleNumber;
                        id = currentBlocked.id; //updated currentBlocked
                        allProcesses[id] = currentBlocked;
                    }
                }
            }
            //currentBlocked --> --, or currentBlocked --> currentRunning
            else if (currentBlocked.IOburstRemaining != 0 && currentRunning == null) {
                if (currentBlocked.IOcycle == cycleNumber) {
                }
                else {
                    if (currentBlocked.IOburstRemaining > 1) {
                        currentBlocked.IOburstRemaining--;
                        currentBlocked.ioTime++;
                        totalBlockedTime++;
                        int id = currentBlocked.id; //updated currentBlocked
                        allProcesses[id] = currentBlocked;
                    }
                    else if (currentBlocked.IOburstRemaining == 1) {
                        currentBlocked.IOburstRemaining--;
                        currentBlocked.ioTime++;
                        totalBlockedTime++;
                        currentRunning = currentBlocked;
                        currentBlocked = null;
                        CPUrandom = randomInput.nextInt();
                        currentRunning.CPUburstRemaining = randomOS(CPUrandom, currentRunning.b);
                        if (currentRunning.c < currentRunning.CPUburstRemaining) {
                            currentRunning.CPUburstRemaining = currentRunning.c;
                        }
                        currentRunning.state = "running";
                        currentRunning.CPUcycle = cycleNumber;
                        int id = currentRunning.id; //updated currentRunning
                        allProcesses[id] = currentRunning;                      
                    }
                }
            }
            //Others that are unstarted or ready
            for (int m = p + 1; m < allProcesses.length; m++) {
                Process currentOther = allProcesses[m];
                if (currentOther.state.equals("unstarted")) {
                    if (currentOther.a > cycleNumber) { //unstarted and unarrived
                        //Do nothing?
                    }
                    else if (currentOther.a <= cycleNumber){
                        currentOther.state = "ready";
                        currentOther.readyCycle = cycleNumber;
                        int id = currentOther.id; //updated currentUnstarted (ready)
                        allProcesses[id] = currentOther;
                    }
                }
                else if (currentOther.state.equals("ready")) {
                    if (currentOther.readyCycle != cycleNumber) {
                        currentOther.waitingTime++;
                        int id = currentOther.id; //updated currentReady
                        allProcesses[id] = currentOther;
                    }
                }
            }
            cycleNumber++;
        }
    }
    printSummary(original, allProcesses, numProcesses, cycleNumber, totalTurnaroundTime, totalRunningTime, totalBlockedTime, totalWaitingTime);
}

Aucun commentaire:

Enregistrer un commentaire