线程休眠、中断、强制执行与礼让分析
一、线程优先级
1、从理论上来讲,线程的优先级越高越有可能先执行(越有可能先抢占资源)。Thread类中针对于优先级有两个处理方法:
- 设置优先级:public final void setPriority(int newPriority);
- 获取优先级:public final int getPriority();
2、在进行优先级定义的时候都是通过int型的数字来完成的,而对于此数字的选择是在Thread类中定义的。常量如下:
- 最高优先级:public static final int MAX_PRIORITY = 10;
- 中等优先级:public static final int NORM_PRIORITY = 5;
- 最低优先级:public static final int MIN_PRIORITY = 1;
优先级设置实例:
public class ThreadDemo{
public static void main(String[] args) throws Exception{
Runnable run = ()->{
for(int x=0;x<10;x++){
Thread.sleep(10000);// 休眠10毫秒
System.out.printIn(Thread.currentThread().getName()+"执行。");
}
};
Thread threadA = new Thread(run,"线程对象A");
Thread threadB = new Thread(run,"线程对象B");
Thread threadC = new Thread(run,"线程对象C");
threadA.setPriority(Thread.MIN_PRIORITY);
threadA.setPriority(Thread.MIN_PRIORITY);
threadA.setPriority(Thread.MAX_PRIORITY);
threadA.start();
threadB.start();
threadC.start();
// 执行结果:当x=0时,C一定先执行,A和B的执行顺序不确定,之后ABC线程的执行顺序是随机的
}
}
二、线程休眠,sleep()
1、休眠可以使一个线程暂缓处理,休眠时间一到程序就会唤醒继续执行,在Thread类中定义的休眠方法如下:
- 休眠方法一:public static void sleep(long millis) throws InterruptedException;
- 休眠方法二:public static void sleep(long millis,int nanos) throws InterruptedException;
2、在进行休眠的时候有可能会产生中断异常“InterruptedException”,中断异常属于Excepton的子类。
如果有多个线程对象,那么休眠也是有先后顺序的,实例如下:
public class ThreadDemo{
public static void main(String[] args) throws Exception{
Runnable run = ()->{
for(int x=0;x<10;x++){
System.out.printIn(Thread.currentThread().getName()+"、x= "+x);
try{
Thread.sleep(10000);// 休眠10毫秒
}catch(InterruptedException e){
e.printStackTrace();
}
}
};
// 产生5个线程对象
for(int num = 0;num <5;num++){
new Thread(run,"线程对象 - "+num).start();
}
}
}
// 程序执行结果,看似程序对象是一起休眠的,一起自动唤醒的
// 但实际上不是,是有先后顺序的,只是程序执行太快
// 线程对象-0、x=0
// 线程对象-3、x=0
// 线程对象-2、x=0
// 线程对象-1、x=0
// 线程对象-4、x=0
// 线程对象-0、x=0
// 线程对象-4、x=1
// 线程对象-2、x=1
// 线程对象-1、x=1
// 线程对象-3、x=1
// ......
程序执行使用图例分析如下:
三、线程中断,interrupt()
1、线程中断指的是别人可以打断自己某个线程的休眠,所有正在执行的线程都是被中断的,中断线程必须进行异常的处理。在Thread类中提供有这种中断执行的处理方法:
- 判断线程是否被中断:public boolean isInterrupted();
- 中断线程执行:public void interrupt();
中断处理操作分析:
public class ThreadDemo{
public static void main(String[] args) throws Exception{
Thread thread = new Thread(()->{
System.out.printIn("执行");
try{
Tread.sleep(10000); // 在不中断的情况下会休眠10秒
}catch(InterrruptedException e){
System.out.printIn("已经执行了中断操作");
}
System.out.printIn("休眠完毕");
});
thread.start(); // 开始休眠
Thread.sleep(1000);
if(!thread.isInterrupted()){ // 若线程未中断,则中断
// 注意,中断就是打断休眠继续执行程序,而不是不再执行未完成的程序
thread.interrupt(); // 执行中断
}
}
}
四、线程强制运行,join()
线程强制运行指的是当满足某些于某些条件之后,某一个线程对象可以一直独占资源,一直到该线程的程序执行结束。
第一步:先观察正常执行的程序
public class ThreadDemo{
public static void main(String[] args) throws Exception{
Thread thread = new Thread(()->{
for(int x = 0;x <100;x++){
System.out.printIn(Thread.currentThread().getName()+"、x= "+x);
}
},"子线程");
thread.start();
// 产生5个线程对象
for(int num = 0;num <100;num++){
System.out.printIn("主线程"+num);
}
//执行结果:子线程与主线程一直抢占资源,主线程和子线程都在交替执行
}
}
第二步:让主线程独占执行,使用Thread类中的强制执行方法join()
public class ThreadDemo{
public static void main(String[] args) throws Exception{
Thread mainThread = Thread.currentThread(); // 获得主线程
Thread thread = new Thread(()->{
for(int x = 0;x <100;x++){
if(x == 3){
mainThread.join(); // 使主线程强制执行
}
System.out.printIn(Thread.currentThread().getName()+"、x= "+x);
}
},"子线程");
thread.start();
// 产生5个线程对象
for(int num = 0;num <100;num++){
Thread.sleep(100); // 休眠0.1s方便观察输出结果
System.out.printIn("主线程"+num);
}
// 执行结果:当x=3的时候,就强制执行主线程,要主线程的所有程序执行完后才执行子线程
}
}
五、线程礼让,yield()
1、线程礼让指的是先将资源让出去让别的线程先执行,但每次礼让执行的时候只礼让一次。使用Thread类中的yield()方法
- 礼让:public static void yield();
礼让实例:
public class ThreadDemo{
public static void main(String[] args) throws Exception{
Thread thread = new Thread(()->{
for(int x = 0;x <100;x++){
if(x%3 == 0){
thread.yield(); // 线程礼让,让别的线程先执行
}
System.out.printIn(Thread.currentThread().getName()+"、x= "+x);
}
},"子线程");
thread.start();
// 产生5个线程对象
for(int num = 0;num <100;num++){
Thread.sleep(100); // 休眠0.1s方便观察输出结果
System.out.printIn("主线程"+num);
}
// 执行结果:如果x能被3整除,就让主线程的程序执行一次再执行子线程的程序
}
}
六、总结
1、强制执行join()与礼让yield()的区别如下:
以线程A和线程B为例,在执行线程A的时候,
若使用了jon(),那么线程A就停止让线程B先执行,一直到线程B的所有程序执行完毕后才能继续执行线程;
若使用了yield(),那么线程A就停止让线程B执行,但是只会让线程B执行一次,然后继续执行线程A的程序。