线程的优先级、Thread.yield()、Thread.sleep(5000)

本文通过示例代码展示了如何设置线程的优先级,并探讨了优先级对线程调度的影响,同时提醒开发者不要随意更改线程优先级以避免影响垃圾回收机制。

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

改变线程的优先级

线程的优先级只要知道就可以了,用默认的5级就足够。

千万不要改变线程的优先级,否则若是虚拟机中垃圾回收机制被饿死了不能执行,导致虚拟机内存耗尽崩溃。

任意一个项目

main方法:

public class Main {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Thread1 t1 = new Thread1("小黑线程-最低优先级");
		Thread2 t2 = new Thread2("小红线程-最高优先级");
		// 改变线程的优先级
		// 线程的优先级只要知道就可以了,用默认的5级就足够,千万不要改变线程的优先级
		t1.setPriority(Thread.MIN_PRIORITY);
		t2.setPriority(10);
		t1.start();
		t2.start();
	}
}
线程1:

public class Thread1 extends Thread {
	public Thread1(String name) {
		super(name);
	}
	// 我们把线程要执行的操作写在run方法中
	// run()方法又被称为线程体
	// 取得优先级并显示
	// 优先级越高被线程调度器选中执行的机会越高
	public void run() {
		for (int i = 1; i <= 100000; i++) {
			System.out.println(this.getName() + "在计数:i=" + i + ",线程的优先级是" + this.getPriority());
		}
	}
}

线程2:

public class Thread2 extends Thread {
	public Thread2(String name) {
		super(name);
	}
	// 我们把线程要执行的操作写在run方法中
	// run()方法又被称为线程体
	public void run() {
		for (int i = 1; i <= 100000; i++) {
			System.out.println(this.getName() + "在计数:i=" + i + ",线程的优先级是" + this.getPriority());
		}
	}
}
上面代码可以执行测试改变线程的优先级。

Thread.yield();//注意:线程的sleep()/yield()方法都是静态方法.静态方法调用的时候和对象无关 

暂停当前正在执行的线程.该线程重新回到了就绪队列,又有可能被线程调度器重新选中。
即使这样的话,相对于其他的线程来说,也是让其他的线程更多的获得了运行机会。

任意一个项目

main方法:

public class Main {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Thread1 t1 = new Thread1("小黑线程-最低优先级");
		Thread2 t2 = new Thread2("小红线程-最高优先级");
		// 改变线程的优先级
		// 线程的优先级只要知道就可以了,用默认的5级就足够,千万不要改变线程的优先级
		t1.setPriority(Thread.MIN_PRIORITY);
		t2.setPriority(10);
		t1.start();
		t2.start();
	}
}
线程1:

public class Thread1 extends Thread {
	public Thread1(String name) {
		super(name);
	}
	// 我们把线程要执行的操作写在run方法中
	// run()方法又被称为线程体
	// 取得优先级并显示
	// 优先级越高被线程调度器选中执行的机会越高
	public void run() {
		for (int i = 1; i <= 100000; i++) {
			System.out.println(this.getName() + "在计数:i=" + i + ",线程的优先级是" + this.getPriority());
		}
	}
}

线程2:

public class Thread2 extends Thread {
	public Thread2(String name) {
		super(name);
	}
	// 我们把线程要执行的操作写在run方法中
	// run()方法又被称为线程体
	public void run() {
		for (int i = 1; i <= 100000; i++) {
			System.err.println(this.getName() + "在计数:i=" + i + ",线程的优先级是"
					+ this.getPriority());
			// =====================================================
			// 注意:线程的sleep()/yield()方法都是静态方法.静态方法调用的时候和对象无关
			// 暂停当前正在执行的线程.该线程重新回到了就绪队列,又有可能被线程调度器重新选中
			// 即使这样的话,相对于其他的线程来说,也是让其他的线程更多的获得了运行机会
			Thread.yield();
		}
	}
}
上面代码可以执行测试Thread.yield();让其他的线程更多的获得了运行机会
Thread.sleep(5000);// 在哪个线程中调用类线程的sleep(),哪个线程就会睡觉

任意一个项目

main方法:

public class Main {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Thread1 t1 = new Thread1("小黑线程-最低优先级");
		Thread2 t2 = new Thread2("小红线程-最高优先级");
		try {
			// 注意:线程的sleep()/yield()方法都是静态方法.静态方法调用的时候和对象无关
			// 在哪个线程中调用类线程的sleep(),哪个线程就会睡觉
			// 下面的代码是在主线程中调用了线程的sleep(),则主线程睡觉
			t1.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		// 改变线程的优先级
		// 线程的优先级只要知道就可以了,用默认的5级就足够,千万不要改变线程的优先级
		t1.setPriority(Thread.MIN_PRIORITY);
		t2.setPriority(10);
		t1.start();
		t2.start();
	}
}
线程1:

public class Thread1 extends Thread {
	public Thread1(String name) {
		super(name);
	}
	// 我们把线程要执行的操作写在run方法中
	// run()方法又被称为线程体
	// 取得优先级并显示
	// 优先级越高被线程调度器选中执行的机会越高
	public void run() {
		for (int i = 1; i <= 100000; i++) {
			System.out.println(this.getName() + "在计数:i=" + i + ",线程的优先级是" + this.getPriority());
		}
	}
}

线程2:

public class Thread2 extends Thread {
	public Thread2(String name) {
		super(name);
	}
	// 我们把线程要执行的操作写在run方法中
	// run()方法又被称为线程体
	public void run() {
		for (int i = 1; i <= 100000; i++) {
			System.err.println(this.getName() + "在计数:i=" + i + ",线程的优先级是"
					+ this.getPriority());
			// =====================================================
			// 注意:线程的sleep()/yield()方法都是静态方法.静态方法调用的时候和对象无关
			// 在哪个线程中调用类线程的sleep(),哪个线程就会睡觉
			try {
				// 线程在睡觉期间不会回到就绪队列.该线程睡醒之后,才会重新回到就绪队列
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
上面代码可以执行测试Thread.sleep(5000);// 在哪个线程中调用类线程的sleep(),哪个线程就会睡觉。

希望对你有帮助,祝你有一个好心情,加油!

若有错误、不全、可优化的点,欢迎纠正与补充;转载请注明出处!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值