当前位置:网站首页>线程的调度(优先级)
线程的调度(优先级)
2022-04-23 07:22:00 【z754916067】
线程的调度
策略
抢占式:高优先级的线程抢占CPU
调度方法
- 对同优先级线程组成先进先出队列(先到先服务),使用时间片策略。
- 对高优先级,使用优先调度的抢占式策略
线程的优先级等级
三个常量:
- MAX_PRIORITY:10
- MIN_PRIORITY:1
- NORM_PRIORITY:5 默认的优先级
函数方法
- getPriority():返回线程优先级
- setPriority(int newPriority):改变线程的优先级
说明
线程创建时继承父线程的优先级
低优先级只是获得调度的概率低,并非一定是在高优先级线程之后才被调用。
代码:创建三个窗口卖票 总数100张
class MyWindow extends Thread{
//票
private static int ticket = 100;
@Override
public void run() {
while (true){
if(ticket>0){
System.out.println(getName()+":卖票,票号为:"+ticket);
ticket--;
}else break;
}
}
}
public class WindowTest {
public static void main(String[] args) {
MyWindow t1 = new MyWindow();
MyWindow t2 = new MyWindow();
MyWindow t3 = new MyWindow();
t1.setName("窗口1");
t2.setName("窗口2");
t3.setName("窗口3");
t1.start();
t2.start();
t3.start();
}
}
存在线程安全问题
版权声明
本文为[z754916067]所创,转载请带上原文链接,感谢
https://blog.csdn.net/z754916067/article/details/124350870
边栏推荐
猜你喜欢

mysql查询字符串类型的字段使用数字类型查询时问题

AAAI 2022 recruit speakers!!

vslam PPT
![[learning] audio and video development from scratch (9) -- nuplayer](/img/62/20b0d80088181fb6ff1fe842500c0a.png)
[learning] audio and video development from scratch (9) -- nuplayer

校园转转二手市场源码下载

freertos学习02-队列 stream buffer message buffer

dried food! Point based: differentiable Poisson solver

一篇文章看懂变量提升(hoisting)

总线结构概述

【解释】get ORA-12838: cannot read/modify an object after modifying it in parallel
随机推荐
396. Rotate Function
Comparison of indoor positioning methods of several intelligent robots
The following program deletes n consecutive words starting from the ith character from the string str
LeetCode15. Sum of three
C language learning record -- use and analysis of string function (2)
thinkphp6+jwt 实现登录验证
LeetCode简单题之计算字符串的数字和
室内定位技术对比
Smart business card applet business card details page function implementation key code
sql 使用过的查询语句
Positioning of high precision welding manipulator
Compiler des questions de principe - avec des réponses
Distributed service governance Nacos
为什么会存在1px问题?怎么解决?
idea:使用easyYapi插件导出yapi接口
AQS & ReentrantLock 实现原理
npm安装yarn
基于TCP/IP协议的网络通信实例——文件传输
多目视觉SLAM
[learning] audio and video development from scratch (9) -- nuplayer