0

これは私のコードです。runメソッドでわかるように、tStart、tEnd、tAround、およびwTimeに値を割り当てます。ただし、スレッドが終了しても、デフォルト値は-1のままです。run()の実行中にそれらの値を出力してみましたが、正しい値があります。ただし、スレッドが終了したときに、これらの値を変数に「書き戻す」ことはありません。

public class PCB extends Thread{
    public int id, arrivalTime, cpuBurst, ioBurst;
    public int tStart, tEnd, tAround, wTime;
    public PCB(){
        id = -1;
        arrivalTime = -1;
        cpuBurst = -1;
        ioBurst = -1;

        tStart = -1;
        tEnd = -1;
        tAround = -1;
        wTime = -1;
    }

 public void run(){
        try{
    .........

            //calculation for FCFS 
            if (id == 1){ //special case for the first one
                tStart = arrivalTime;
            }
            else tStart = lastEndTime;

            tEnd = tStart + cpuBurst + ioBurst;
            tAround = tEnd - arrivalTime;
            wTime = tStart - arrivalTime;

                            PCBThreadStopFlag = true;   

        }
        catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}

スレッドが終了すると、次のように値を出力します。

        // now we print out the process table
    String format = "|P%1$-10s|%2$-10s|%3$-10s|%4$-10s|%5$-10s|%6$-10s|%7$-10s|%8$-10s|\n";
    System.out.format(format, "ID", "ArrTime", "CPUBurst", "I/OBurst", "TimeStart", "TimeEnd","TurnAround","WaitTime");
    ListIterator<PCB> iter = resultQueue.listIterator();
    while(iter.hasNext()){
        PCB temp = iter.next();
        System.out.format(format, temp.id, temp.arrivalTime, temp.cpuBurst, temp.ioBurst, temp.tStart, temp.tEnd, temp.tAround, temp.wTime );
    }

そして、これが私が最初にスレッドが停止するのを待った方法です:

while(!rq.values.isEmpty()){
            //System.out.println("Ready queue capacity now: " + rq.values.size());
            currentProcess = new PCB(rq.values.getFirst());
            currentProcess.start();

            while(PCBThreadStopFlag == false) {}
            //currentProcess.stop();
            PCBThreadStopFlag = false;

            //after everything is done, remove the first pcb
            // and add it to the result queue (just to print the report)
            resultQueue.addLast(rq.values.removeFirst());           
        }

run()の上部(すべての割り当てが完了した最後)でフラグPCBThreadStopFlagを使用し、この関数でwhile(PCBThreadStopFlag == false){}を使用して「ビジーウェイト」タスクを実行します。これが原因かもしれませんか?

4

1 に答える 1

3

これは単なる推測ですが、結果を印刷する前にスレッドに参加していないに違いありません。つまり、スレッドを開始し、スレッドが完了するのを待たずにすぐに結果を出力しているのではないかと思います。

編集: OK、これを試してください...

アイデア#1:PCBThreadStopFlagを揮発性として宣言し、再試行します。それがうまくいくかどうか教えてください。

アイデア#2:停止フラグ全体を完全に取り除き、ビジーウェイトを次のように置き換えます

currentProcess.join();

それがうまくいくかどうか教えてください。

于 2009-11-01T05:39:47.477 に答える