线程的优先级,线程的休眠

线程的优先级:

我们知道线程是并发的,是随机的,先抢到cpu资源的线程就先执行,但是可以通过改变线程优先级的方式来影响线程执行的先后顺序。

原理上说线程优先级越高则越先执行,但是这只是线程优先级高的先被执行的概率增大而已,不是线程优先级高的一定先执行。

操作线程优先级的方法:

public final int getPriority()
【取得线程的优先级】

public final void setPriority(int newPriority)
【设置线程的优先级】

在Thread类中提供了三种优先级:
最高优先级:public static final int MAX_PRIORITY =10
中等优先级:public static final int NORM_PRIORITY= 5
最低优先级:public static final int MIN_PRIORITY = 1

设置优先级:

public class Test {
	public static void main(String[] args){
		//创建线程主体类对象
		Runnable  rn=new RunThread();
		//创建了三个线程对象,这三个线程对象共享一个线程主体类对象
		Thread    thA=new Thread(rn,"线程A");
		Thread    thB=new Thread(rn,"线程B");
		Thread    thC=new Thread(rn,"线程C");
		Thread    thD=new Thread(rn,"线程D");
		Thread    thE=new Thread(rn,"线程E");
		//设置线程A的优先级(设置为最低)
		thA.setPriority(Thread.MIN_PRIORITY);
		thB.setPriority(Thread.MAX_PRIORITY);
		thA.start();
		thB.start();
		thC.start();
		thD.start();
		thE.start();
	}
}

在这里插入图片描述
此时B线程的优先级是最高的,A线程的优先级是最低的。但是没有一定B先执行,A最后执行,证明了不是优先级最高的一定先执行,只是增大了概率。

获取线程的优先级:

class  RunThread  implements  Runnable{
	@Override
	public void run() {
		 System.out.println(Thread.currentThread().getName()+"优先级是:"+Thread.currentThread().getPriority());
	}
}

发现了默认优先级是5

package com.sun.test;
public class Test {
	public static void main(String[] args) throws Exception {
	    System.out.println(Thread.currentThread().getPriority());
	}
}

在这里插入图片描述
获取主线程的优先级


public class Test {
	public static void main(String[] args){
		 System.out.println(Thread.currentThread().getPriority());
	}
}

总结:

1、线程都有自己的优先级,但是不是优先级高的就一定先执行,只是增大了先执行的概率

线程的休眠:

线程的休眠就是让线程暂不执行,之后在指定时间后恢复正常的执行,这就是线程的休眠,线程的休眠需要使用到的方法:

public static void sleep(long millis) throws InterruptedException
【让线程休眠,long millis表示休眠的时间,休眠时间完了之后会自动醒来】

让主方法休眠

主方法是一个主线程,也可以让其休眠

public class Test {
	public static void main(String[] args) throws InterruptedException{
		 System.out.print("Hello ");
		 Thread.sleep(2000);
		 System.out.print("world ! ");
	}
}

在这里插入图片描述

其他线程的休眠(子线程)

如果一个线程启动了则一定会执行 线程主体类的run()方法,此时就考虑在run方法中实现线程的休眠

package com.sun.test;

class RunThread implements Runnable {
	@Override
	public void run() {
		// 让线程休眠
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName());
	}
}
public class Test {
	public static void main(String[] args) throws InterruptedException {
		// 创建线程主体类对象
		Runnable rn = new RunThread();
		// 创建了三个线程对象,这三个线程对象共享一个线程主体类对象
		Thread thA = new Thread(rn, "线程A");
		Thread thB = new Thread(rn, "线程B");
		Thread thC = new Thread(rn, "线程C");
		Thread thD = new Thread(rn, "线程D");
		Thread thE = new Thread(rn, "线程E");
		thA.start();
		thB.start();
		thC.start();
		thD.start();
		thE.start();
	}
}

在这里插入图片描述

发现了所有线程都休眠了大概两秒时间之后再执行代码,但是所有的线程都是同时休眠的吗?不可能是同时休眠的,因为线程是并发,随机的,谁先抢到cpu资源谁先执行,先执行的先休眠,之所以你看到是一起休眠的是因为时间差太小你肉眼没观察到。

让线程A休眠其他线程不休眠

class RunThread implements Runnable {
	@Override
	public void run() {
	 if("线程A".equals(Thread.currentThread().getName())) {
		// 让线程休眠
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	 }
	 System.out.println(Thread.currentThread().getName());
	}
}

总结:

1、线程的暂缓执行就是休眠,使用的方法是sleep(),休眠之后会自动醒来执行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值