继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

操作系统之时间片轮转算法

Lian_Huang
关注TA
已关注
手记 5
粉丝 4
获赞 72

//时间片轮转算法
class ListQueue {
// 链队列
class Link {
private String name;
private int sertime;
private int priority;
public Link next;
public Link() {
}
public Link(String name, int sertime, int priority) {
this.name = name;
this.sertime = sertime;
this.priority = priority;
}
public void displayLink() {
System.out.println("进程名 " + name + "服务时间 " + sertime + "优先级"

  • priority);
    }
    }
    public static Link head;
    public static Link rear;
    public boolean isEmptyQueue() {
    return head == null;
    }
    public ListQueue() {
    head = rear = null;
    }
    // 从尾部插入
    public void insertToQueue(String name, int sertime, int priority) {
    Link newLink = new Link(name, sertime, priority);
    // 第一次的链接点为head端
    if (isEmptyQueue()) {
    head = newLink;
    } else {
    rear.next = newLink;
    }
    rear = newLink;
    }
    // 从头部删除
    public void removeFromQueue() {
    if (isEmptyQueue()) {
    System.out.println("Queue is empty");
    } else {
    if (null == head.next) {
    rear = null;
    }
    head = head.next;
    }
    }
    public void displayQueue() {
    Link currentLink = head;
    while (null != currentLink) {
    currentLink.displayLink();
    currentLink = currentLink.next;
    }
    }
    public static void main(String[] args) {
    int i = 0;
    ListQueue lq = new ListQueue();
    // 插入数据
    lq.insertToQueue("A", 3, 12);
    lq.insertToQueue("B", 5, 31);
    lq.insertToQueue("C", 2, 21);
    lq.insertToQueue("D", 4, 10);
    System.out.println("时间片轮转算法:");
    for (Link visitLink = head; visitLink != null; visitLink = visitLink.next) {
    System.out.print("t=" + i + " " + visitLink.name + "执行");
    if (visitLink.sertime <= 2) {
    System.out.println(" 完成任务");
    lq.removeFromQueue();
    } else {
    System.out.println(" 未完成任务");
    lq.insertToQueue(visitLink.name, visitLink.sertime - 2,
    visitLink.priority);
    lq.removeFromQueue();
    }
    i++;
    }
    }
    }
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP