线程的调度

本文介绍了线程的调度,包括线程的优先级等级,如MAX_PRIORITY、MIN_PRIORITY和NORM_PRIORITY,以及如何通过getPriority()获取和setPriority()设置线程优先级。线程在创建时会继承父线程的优先级,但高优先级并不保证优先执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线程的调度

在这里插入图片描述
在这里插入图片描述

线程的优先级等级

MAX_PRIORITY:10
MIN_PRIORITY:1
NORM_PRIORITY:5

线程优先级涉及的方法

getPriortiy():返回线程的优先级
setPriority(int newPriortiy):改变线程的优先级

线程创建时继承父线程的优先级
第优先级只是获得调度的概率低,并非一定是在高优先级线程之后才被调用。

package threadStudy;

public class Main {
	public static void main(String[] args) {
		MyThread mythread = new MyThread("MyThread");
		mythread.setPriority(Thread.MAX_PRIORITY);
		mythread.start();
		
		
		Thread.currentThread().setName("MainThread");
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

		for(int i = 0; i <= 100; i++){
			System.out.println(Thread.currentThread().getName()+"-"+i);
		}
		
		
	}
}

package threadStudy;

public class MyThread extends Thread{
	
	private String threadName;
	
	public MyThread(String threadName){
		super(threadName);
	}
	
	public void run(){
		for(int i = 0; i <= 100; i++){
			System.out.println(this.currentThread().getName()+"***"+i);
		}
		
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值