在先到先得(FCFS)算法中实现线程

过去2天,我一直在努力解决这个问题,但似乎无法解决。我有一项任务,要求我们在单个线程程序中实现“先来先服务(FCFS)”算法。我们必须要使用一个名为processData.txt的文本文件,其中包含进程名称,到达时间,突发时间和优先级值。格式如下:

P1,20,40
P2,25,30

我正要在不使用线程的情况下运行程序。

任务的下一部分是创建一个线程,将其称为调度程序,并让该线程执行算法。

我可以在不使用任何线程的情况下运行以下代码:

class FCFS { 

    //driver code
    public static void main(String[] args) throws ParseException,FileNotFoundException { 

        //import values from file
        File file = new File("C:\\some\\path\\processData.txt");
        Scanner sc = new Scanner(file);
        sc.useDelimiter("P|,|\r|  +");

        ArrayList<Integer> list = new ArrayList<>();
        while(sc.hasnextLine()){
            if(sc.hasnextInt()){
                list.add(sc.nextInt());
            }
            else sc.next();
        }
        sc.close();

        //System.out.println(list);
        int processes[] = {list.get(0),list.get(4)};
        int arrival[] = {list.get(1),list.get(5)};
        int burst[] = {list.get(2),list.get(6)};
        int priority[] = {list.get(3),list.get(7)};

        int n = processes.length;
        findavgTime(processes,n,burst);   
    }

    // Function to find the waiting time for all processes  
    static void findWaitingTime(int processes[],int n,int bt[],int wt[]) { 
        // waiting time for first process is 0  
        wt[0] = 0;

        // calculating waiting time  
        for (int i = 1; i < n; i++) { 
            wt[i] = bt[i - 1] + wt[i - 1]; 
        } 
    } 

    // Function to calculate turn around time  
    static void findTurnAroundTime(int processes[],int wt[],int tat[]) { 
        // calculating turnaround time by adding  
        // bt[i] + wt[i]  
        for (int i = 0; i < n; i++) { 
            tat[i] = bt[i] + wt[i]; 
        } 
    } 

    //Function to calculate average time  
    static void findavgTime(int processes[],int bt[]) { 
        int wt[] = new int[n],tat[] = new int[n]; 
        int total_wt = 0,total_tat = 0; 

        //Function to find waiting time of all processes  
        findWaitingTime(processes,bt,wt); 

        //Function to find turn around time for all processes  
        findTurnAroundTime(processes,wt,tat); 

        //Display processes along with all details  
        System.out.printf("Processes    Burst time     Waiting"
                       +" time     Turn around time\n"); 

        // Calculate total waiting time and total turn around time  
        for (int i = 0; i < n; i++) { 
            total_wt = total_wt + wt[i]; 
            total_tat = total_tat + tat[i]; 
            System.out.printf("     P%d ",(i + 1)); 
            System.out.printf("           %d ",bt[i]); 
            System.out.printf("             %d",wt[i]); 
            System.out.printf("                %d\n",tat[i]); 
        } 
        float s = (float)total_wt /(float) n; 
        int t = total_tat / n; 
        //System.out.printf("Average waiting time = %f",s); 
        //System.out.printf("\n"); 
        //System.out.printf("Average turn around time = %d ",t); 
    } 
} 

我一直在努力使线程正常工作,这是我到目前为止的工作,我似乎无法弄清楚我应该做什么。我意识到run()中的内容不正确,但是我很迷茫。

到目前为止:

public class Project { 

    public static void main(String[] args) throws ParseException,|\r|  +");

        ArrayList<Integer> list = new ArrayList<>();
        while(sc.hasnextLine()){
            if(sc.hasnextInt()){
                list.add(sc.nextInt());
            }
            else sc.next();
        }
        sc.close();

        FCFS a = new FCFS(list);
        Thread scheduler = new Thread(a);
        scheduler.start();  
    }
}

class FCFS implements Runnable{
    private final ArrayList<Integer> list;

    public FCFS(ArrayList<Integer>list){
       this.list = list;
       int processes[] = {list.get(0),list.get(4)};
       int arrival[] = {list.get(1),list.get(5)};
       int burst[] = {list.get(2),list.get(6)};
       int priority[] = {list.get(3),list.get(7)};
       int n = processes.length;
    }

    @Override
    public void run(){
        findavgTime(int processes,burst);
    }

    // Function to find the waiting time for all processes  
    static void findWaitingTime(int processes[],t); 
    } 
} 

希望有人可以指出我的方法可以为我运行或修复我的代码的方向。谢谢。

lz245548622 回答:在先到先得(FCFS)算法中实现线程

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3127672.html

大家都在问