ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java线程的优先级和执行顺序

2019-07-11 21:01:37  阅读:155  来源: 互联网

标签:Java Thread SecondThreadInput FirstThreadInput run 线程 优先级


在学习运算符时,读者知道各个运算符之间有优先级,了解运算符的优先级对程序幵发有很好的作用。线程也是如此,每个线程都具有优先级,Java 虚拟机根据线程的优先级决定线程的执行顺序,这样使多线程合理共享 CPU 资源而不会产生冲突。

优先级概述

在 Java 语言中,线程的优先级范围是 1~10,值必须在 1~10,否则会出现异常;优先级的默认值为 5。优先级较高的线程会被优先执行,当执行完毕,才会轮到优先级较低的线程执行。如果优先级相同,那么就采用轮流执行的方式。

可以使用 Thread 类中的 setPriority() 方法来设置线程的优先级。语法如下:

public final void setPriority(int newPriority);


如果要获取当前线程的优先级,可以直接调用 getPriority() 方法。语法如下:

public final int getPriority();

使用优先级

简单了解过优先级之后,下面通过一个简单的例子来演示如何使用优先级。

例 1

分别使用 Thread 类和 Runnable 接口创建线程,并为它们指定优先级。

(1) 创建继承自 Thread 类的 FirstThreadInput 类,重写该类的 run() 方法。代码如下:


 
  1. package ch14;
  2. public class FirstThreadInput extends Thread
  3. {
  4. public void run()
  5. {
  6. System.out.println("调用FirstThreadInput类的run()重写方法"); //输出字符串
  7. for(int i=0;i<5;i++)
  8. {
  9. System.out.println("FirstThreadInput线程中i="+i); //输出信息
  10. try
  11. {
  12. Thread.sleep((int) Math.random()*100); //线程休眠
  13. }
  14. catch(Exception e){}
  15. }
  16. }
  17. }


(2) 创建实现 Runnable 接口的 SecondThreadInput 类,实现 run() 方法。代码如下:


 
  1. package ch14;
  2. public class SecondThreadInput implements Runnable
  3. {
  4. public void run()
  5. {
  6. System.out.println("调用SecondThreadInput类的run()重写方法"); //输出字符串
  7. for(int i=0;i<5;i++)
  8. {
  9. System.out.println("SecondThreadInput线程中i="+i); //输出信息
  10. try
  11. {
  12. Thread.sleep((int) Math.random()*100); //线程休眠
  13. }
  14. catch(Exception e){}
  15. }
  16. }
  17. }


(3) 创建 TestThreadInput 测试类,分别使用 Thread 类的子类和 Runnable 接口的对象创建线程,然后调用 setPriority() 方法将这两个线程的优先级设置为 4,最后启动线程。代码如下:


 
  1. package ch14;
  2. public class TestThreadInput
  3. {
  4. public static void main(String[] args)
  5. {
  6. FirstThreadInput fti=new FirstThreadInput();
  7. Thread sti=new Thread(new SecondThreadInput());
  8. fti.setPriority(4);
  9. sti.setPriority(4);
  10. fti.start();
  11. sti.start();
  12. }
  13. }


(4) 运行上述代码,运行结果如下所示。

调用FirstThreadInput类的run()重写方法
调用SecondThreadInput类的run()重写方法
FirstThreadInput线程中i=0
SecondThreadInput线程中i=0
FirstThreadInput线程中i=1
FirstThreadInput线程中i=2
SecondThreadInput线程中i=1
FirstThreadInput线程中i=3
SecondThreadInput线程中i=2
FirstThreadInput线程中i=4
SecondThreadInput线程中i=3
SecondThreadInput线程中i=4


由于该例子将两个线程的优先级都设置为 4,因此它们交互占用 CPU ,宏观上处于并行运行状态。

重新更改 ThreadInput 类的代码、设置优先级。代码如下:


 
  1. fti.setPriority(1);
  2. sti.setPriority(10);


重新运行上述代码,如下所示。

调用FirstThreadInput类的run()重写方法
调用SecondThreadInput类的run()重写方法
FirstThreadInput线程中i=0
SecondThreadInput线程中i=0
SecondThreadInput线程中i=1
SecondThreadInput线程中i=2
SecondThreadInput线程中i=3
SecondThreadInput线程中i=4
FirstThreadInput线程中i=1
FirstThreadInput线程中i=2
FirstThreadInput线程中i=3
FirstThreadInput线程中i=4

标签:Java,Thread,SecondThreadInput,FirstThreadInput,run,线程,优先级
来源: https://blog.csdn.net/jijiangpeng/article/details/95519951

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有