代码块运行的优先级

package FatherAndSon;

/*
* 父类
*/
public class Father {

static {
System.out.println("这是父类中的静态代码块");
}

public Father() {
System.out.println("这是父类中的构造方法");
}

{
System.out.println("这是父类中的构造代码块1");
}
}

-----------------以下是子类-------------------------


package FatherAndSon;
/*
* 子类
*/
public class Son extends Father {
static {
System.out.println("这是子类中的静态代码块");
}

{
System.out.println("这是子类中的构造代码块");
}

public Son() {
System.out.println("这是子类中的构造方法");
}
}

--------------------测试打印------------------

package FatherAndSon;

public class Test01 {
public static void main(String[] args) {
Son son = new Son();
}
}

-----------------打印结果----------------------

这是父类中的静态代码块
这是子类中的静态代码块
这是父类中的构造代码块1
这是父类中的构造方法
这是子类中的构造代码块
这是子类中的构造方法

转载于:https://www.cnblogs.com/Koma-vv/p/9546823.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的抢占式优先级调度算法的 C 代码实现: ```c #include <stdio.h> #include <stdlib.h> // 进程控制块结构体 typedef struct { int pid; // 进程 ID int priority; // 进程优先级 int burst_time; // 进程运行时间 int remaining_time; // 进程剩余运行时间 } PCB; // 创建进程控制块 PCB* create_process(int pid, int priority, int burst_time) { PCB* process = (PCB*) malloc(sizeof(PCB)); process->pid = pid; process->priority = priority; process->burst_time = burst_time; process->remaining_time = burst_time; return process; } // 释放进程控制块 void destroy_process(PCB* process) { free(process); } // 优先级比较函数 int priority_compare(const void* a, const void* b) { PCB* process_a = *(PCB**) a; PCB* process_b = *(PCB**) b; return process_b->priority - process_a->priority; } int main() { // 创建进程 PCB* p1 = create_process(1, 3, 6); PCB* p2 = create_process(2, 1, 4); PCB* p3 = create_process(3, 4, 3); PCB* p4 = create_process(4, 2, 5); // 将进程放入进程队列 PCB* processes[] = {p1, p2, p3, p4}; int num_processes = 4; // 模拟调度 int time_slice = 1; int current_time = 0; while (num_processes > 0) { // 按优先级排序 qsort(processes, num_processes, sizeof(PCB*), priority_compare); // 选择优先级最高的进程运行 PCB* current_process = processes[0]; current_process->remaining_time -= time_slice; current_time += time_slice; // 如果进程已经运行完毕,移除进程 if (current_process->remaining_time <= 0) { printf("进程 %d 运行完毕,用时 %d\n", current_process->pid, current_time); for (int i = 0; i < num_processes - 1; i++) { processes[i] = processes[i + 1]; } num_processes--; destroy_process(current_process); } } return 0; } ``` 该程序创建了 4 个进程,使用抢占式优先级调度算法模拟进程调度过程。在每个时间片中,程序按照进程优先级排序,选择优先级最高的进程运行,直到进程运行完毕。程序输出每个进程的运行时间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值