当前位置:网站首页>线程的调度(优先级)
线程的调度(优先级)
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
边栏推荐
猜你喜欢

dried food! Point based: differentiable Poisson solver

idea:使用easyYapi插件导出yapi接口

为什么会存在1px问题?怎么解决?

AAAI 2022招募讲者啦!!

2022.4.11-4.17 AI行业周刊(第93期):AI行业的困局

396. Rotate Function

如何在SQL Server中导入excel数据,2019版

Detailed explanation of ansible automatic operation and maintenance (I) installation and deployment, parameter use, list management, configuration file parameters and user level ansible operating envi

LeetCode简单题之统计字符串中的元音子字符串

Canvas learning Chapter 1
随机推荐
Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction
js将树形结构数据转为一维数组数据
My heart's broken! A woman's circle of friends envied others for paying wages on time and was fired. Even her colleagues who liked her were fired together
Interesting JS code
[untitled]
AAAI 2022 recruit speakers!!
[go]常见的并发模型[泛型版]
怎么读书读论文
1216_ MISRA_ C standard learning notes_ Rule requirements for control flow
Comparison of indoor positioning methods of several intelligent robots
浏览器中的 Kubernetes 和 IDE | 交互式学习平台Killercoda
[Effective Go 中文翻译] 第一篇
C outputs a two-dimensional array with the following characteristics.
LeetCode中等题之旋转函数
The simple problem of leetcode is to calculate the numerical sum of strings
有意思的js 代码
Positioning of high precision welding manipulator
Online yaml to XML tool
Briefly describe the hierarchical strategy of memory
浅谈ES6尾调优化