线程优先级、线程分类(用户线程、守护线程)

约定不等于承诺〃 2022-10-05 10:51 496阅读 0赞

线程优先级

java 中的线程优先级的范围是1~10,默认的优先级是5。“高优先级线程”会优先于“低优先级线程”执行。

贴一段setPriority(int newPriority)源码:

  1. public final static int MIN_PRIORITY = 1;
  2. public final static int NORM_PRIORITY = 5;
  3. public final static int MAX_PRIORITY = 10;
  4. public final void setPriority(int newPriority) {
  5. ThreadGroup g;
  6. checkAccess();
  7. if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
  8. throw new IllegalArgumentException();
  9. }
  10. if((g = getThreadGroup()) != null) {
  11. if (newPriority > g.getMaxPriority()) {
  12. newPriority = g.getMaxPriority();
  13. }
  14. setPriority0(priority = newPriority);
  15. }
  16. }

源码看出,线程优先级范围是0~10,默认是5

获得 线程的优先级:

  1. public final int getPriority() {
  2. return priority;
  3. }

线程优先级demo:

  1. public class TestMyThread {
  2. public static void main(String[] args) {
  3. MyThread mt1 = new MyThread();
  4. MyThread mt2 = new MyThread();
  5. MyThread mt = new MyThread();
  6. mt1.setPriority(1);// 设置线程的优先级为1
  7. mt2.setPriority(10); // 设置线程的优先级10
  8. mt.start(); // 默认情况下线程的优先级为5
  9. mt1.start();
  10. mt2.start();
  11. }
  12. }
  13. class MyThread extends Thread {
  14. @Override
  15. public void run() {
  16. long begin = System.currentTimeMillis();
  17. for (int i = 0; i < 50000000; i++) {
  18. int a=2000*20;
  19. }
  20. long end = System.currentTimeMillis();
  21. long time = end - begin;
  22. System.out.println(Thread.currentThread().getName() + "优先级为:" + this.getPriority() + " 执行完毕,花费时间为" + time); // getPriority()方法可以获取线程的优先级
  23. }
  24. }

运行5次的结果:

Thread-1优先级为:10 执行完毕,花费时间为3
Thread-2优先级为:5 执行完毕,花费时间为28
Thread-0优先级为:1 执行完毕,花费时间为29

Thread-1优先级为:10 执行完毕,花费时间为2
Thread-0优先级为:1 执行完毕,花费时间为31
Thread-2优先级为:5 执行完毕,花费时间为32

Thread-1优先级为:10 执行完毕,花费时间为3
Thread-2优先级为:5 执行完毕,花费时间为24
Thread-0优先级为:1 执行完毕,花费时间为24

Thread-2优先级为:5 执行完毕,花费时间为3
Thread-0优先级为:1 执行完毕,花费时间为22
Thread-1优先级为:10 执行完毕,花费时间为22

Thread-2优先级为:5 执行完毕,花费时间为3
Thread-1优先级为:10 执行完毕,花费时间为25
Thread-0优先级为:1 执行完毕,花费时间为25

结果可以看出:并不是优先级高的线程先于优先级低的线程执行完毕,而是线程的优先级只能确保CPU尽量将执行的资源让给优先级高的线程用,但不保证定义的高优先级的线程的大部分都能先于低优先级的线程执行完,所以不建议设置线程的优先级。

线程分类

线程分类:

  1. 用户线程和守护线程

用户线程和守护线程的区别

二者其实基本上是一样的。唯一的区别在于JVM何时离开。

用户线程:当存在任何一个用户线程未离开,JVM是不会离开的。

守护线程:如果只剩下守护线程未离开,JVM是可以离开的。

在Java中,制作守护线程非常简单,直接利用.setDaemon(true)

用户线程

定义:平时用到的普通线程均是用户线程,当在Java程序中创建一个线程,它就被称为用户线程

守护线程

定义:是个服务线程,准确地来说就是服务其他的线程,这是它的作用——而其他的线程只有一种,那就是用户线程。所以java里线程分2种,
1、守护线程,比如垃圾回收线程,就是最典型的守护线程。
2、用户线程,就是应用程序里的自定义线程

守护线程demo:

  1. class MyThread extends Thread{
  2. public MyThread(String name) {
  3. super(name);
  4. }
  5. public void run(){
  6. try {
  7. for (int i=0; i<5; i++) {
  8. Thread.sleep(3);
  9. System.out.println(this.getName() +"(isDaemon="+this.isDaemon()+ ")" +", loop "+i);
  10. }
  11. } catch (InterruptedException e) {
  12. }
  13. }
  14. };
  15. class MyDaemon extends Thread{
  16. public MyDaemon(String name) {
  17. super(name);
  18. }
  19. public void run(){
  20. try {
  21. for (int i=0; i<10000; i++) {
  22. Thread.sleep(1);
  23. System.out.println(this.getName() +"(isDaemon="+this.isDaemon()+ ")" +", loop "+i);
  24. }
  25. } catch (InterruptedException e) {
  26. }
  27. }
  28. }
  29. public class Demo {
  30. public static void main(String[] args) {
  31. System.out.println(Thread.currentThread().getName()
  32. +"(isDaemon="+Thread.currentThread().isDaemon()+ ")");
  33. Thread t1=new MyThread("t1"); // 新建t1
  34. Thread t2=new MyDaemon("t2"); // 新建t2
  35. t2.setDaemon(true); // 设置t2为守护线程
  36. t1.start(); // 启动t1
  37. t2.start(); // 启动t2
  38. }
  39. }

运行结果:

  1. main(isDaemon=false)
  2. t2(isDaemon=true), loop 0
  3. t2(isDaemon=true), loop 1
  4. t1(isDaemon=false), loop 0
  5. t2(isDaemon=true), loop 2
  6. t2(isDaemon=true), loop 3
  7. t1(isDaemon=false), loop 1
  8. t2(isDaemon=true), loop 4
  9. t2(isDaemon=true), loop 5
  10. t2(isDaemon=true), loop 6
  11. t1(isDaemon=false), loop 2
  12. t2(isDaemon=true), loop 7
  13. t2(isDaemon=true), loop 8
  14. t2(isDaemon=true), loop 9
  15. t1(isDaemon=false), loop 3
  16. t2(isDaemon=true), loop 10
  17. t2(isDaemon=true), loop 11
  18. t1(isDaemon=false), loop 4
  19. t2(isDaemon=true), loop 12

结果说明
(01) 主线程main是用户线程,它创建的子线程t1也是用户线程。
(02) t2是守护线程。在“主线程main”和“子线程t1”(它们都是用户线程)执行完毕,只剩t2这个守护线程的时候,JVM自动退出。

发表评论

表情:
评论列表 (有 0 条评论,496人围观)

还没有评论,来说两句吧...

相关阅读

    相关 线优先级&& 守护线

    一、线程的优先级 java 提供了一个线程调度器来监控程序种启动后进入就绪状态的所有线程,线程调度器按照线程的优先级决定应该调度哪个线程来执行,优先级高的被优先调度。