日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

进程调度算法Java代码

發布時間:2025/3/21 java 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 进程调度算法Java代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一步:創建一個found類

import java.util.ArrayList;class Found {ArrayList<PCB> sequnce;//創建就緒隊列PCB pcb[] = new PCB[5];int StartTime = 0;int SystemTime = (int) (Math.random() * 3) + 1;//隨即產生系統時間Found() {sequnce = new ArrayList<PCB>();for (int i = 0; i < 5; i++) {pcb[i] = new PCB();pcb[i].Id = i + 1;sequnce.add(pcb[i]);}}void FCFS()//先來先服務算法{PCB Running = null;while (sequnce.size() > 0)//就緒隊列不為空{Running = sequnce.remove(0);Running.UseTime = Running.NeedTime;Running.NeedTime = 0;Running.Perior = 0;System.out.println("當前系統時間:" + SystemTime);SystemTime += Running.UseTime;ShowMessages(Running);}}void RR()//時間片輪換算法{PCB Running = null;int Time = SystemTime;while (sequnce.size() > 0) {System.out.println("當前系統時間:" + SystemTime);Running = sequnce.remove(0);if (Running.NeedTime <= Time) {Running.UseTime = Running.NeedTime;Running.NeedTime = 0;Running.Perior = 0;Running.Status = "Finish";SystemTime += Running.UseTime;} else {Running.UseTime += Time;Running.NeedTime -= Time;Running.Perior--;Running.Status = "Ready";sequnce.add(Running);SystemTime += Time;}ShowMessages(Running);}}void ShowMessages(PCB p)//輸出信息{System.out.println("當前運行進程:" + p.Id +" " + "服務時間:" + p.UseTime +" " + "需要時間:" + p.NeedTime +" " + "優先級:" + p.Perior +" " + "狀態:" + p.Status);if (sequnce.size() > 0) {System.out.println("當前就緒進程:");for (PCB p1 : sequnce) {System.out.println("進程編號:" + p1.Id +" " + "服務時間:" + p1.UseTime +" " + "需要時間:" + p1.NeedTime +" " + "優先級:" + p1.Perior +" " + "狀態:" + p1.Status);System.out.println("--------------------------------------------------------------------------");}} else {System.out.println("當前系統中已經沒有就緒進程!");}System.out.println('\n');} }

第二步:創建一個Menu類

import java.util.Scanner;class Menu//主界面菜單 { Scanner sc=new Scanner(System.in);int print() { System.out.println("********************************************");System.out.println(" 進調度算法演示");System.out.println("********************************************");System.out.println(" 1.先來先服務(FCFS)算法");System.out.println(" 2.時間片輪換(RR)算法");System.out.println(" 3.退出該程序");System.out.print("請選擇所要采用的算法:");int flag=sc.nextInt(); return flag; }void select() {int flag=print();switch (flag){ case 1: Found Process1=new Found();Process1.FCFS(); print();case 2: Found Process2=new Found(); Process2.RR(); print();case 3: System.exit(0);default: break; }} }

第三步:創建一個PCB類

import java.util.ArrayList; import java.util.Scanner; public class PCB {int Id;//進程編號int UseTime;//服務時間int NeedTime;//需要時間int Perior;//優先級String Status;//狀態PCB() {Id++;UseTime = 0;NeedTime = (int) Math.round(Math.random() * 6) + 1;//隨機產生需要時間Perior = (int) Math.round(Math.random() * 5) + 1;//隨即產生優先級 Status="Ready";//初始狀態為就緒 } }}}

第四步:創建一個ProcessControl 類

public class ProcessControl {public static void main(String args[]){PCB pcb=new PCB();Menu Tencent=new Menu();Tencent.select();} }

總結

以上是生活随笔為你收集整理的进程调度算法Java代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。