开发者

in Java, I can't assign values to variables while i'm in the run() function of a thread-extended class

This is my code. As you see in the run method, I assign values to tStart, tEnd, tAround and wTime. But when the Thread ends, they still have the default values of -1. I try 开发者_StackOverflowprinting out their values while the run() is running, and I have the correct values. But they are not 'writing' those values back to the variables when the thread ends.

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();
        }
    }
}

When the thread ends, this is how I print out the values:

        // 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 );
    }

And here is how I waited for the thread to stop first:

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());           
        }

I use the flag PCBThreadStopFlag in the run() up top (at the end when all the assignments are done) then in this function, I use while(PCBThreadStopFlag == false) {} to do the "busy-wait" task. may be this is the cause??


This is just a guess, but I'll bet you're not joining on the threads before you print the results. In other words I suspect you're starting the threads and then immediately printing the result without waiting for the threads to complete.

EDIT: OK, try this...

Idea #1: Declare the PCBThreadStopFlag as volatile, and try again. Tell us if that works.

Idea #2: Get rid of the whole stop flag thing altogether, and replace the busy wait with

currentProcess.join();

and tell us if that works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜