本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

大厂之路一由浅入深、并行基础、源码分析一线程优先级、守护线程

发布于2021-05-29 21:09     阅读(1322)     评论(0)     点赞(26)     收藏(4)


  • 线程的优先级
    • java 中的线程优先级的范围是1~10,默认的优先级是5
    • “高优先级线程”会优先于“低优先级线程”执行。
    • JDK中的介绍:
      • Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority.
      • Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.
      • 每个线程都有一个优先级。“高优先级线程”会优先于“低优先级线程”执行。每个线程都可以被标记为一个守护进程或非守护进程。
      • 在一些运行的主线程中创建新的子线程时,子线程的优先级被设置为等于“创建它的主线程的优先级”当且仅当“创建它的主线程是守护线程”时“子线程才会是守护线程”。
      • 源码:
        • private int priority;
        • 在这里插入图片描述
      • 代码示例:
package com.wwj.text;

public class PriorityDemo {
    public static class  HightPriority extends  Thread{
        static  int count = 0;

        @Override
        public void run() {
            while(true){
                synchronized (PriorityDemo.class){
                    count++;
                    if(count>10000000){
                        System.out.println("高优先级输出完成");
                        break;
                    }
                }
            }
        }
    }
    public static class  LowPriority extends  Thread{
        static  int count = 0;

        @Override
        public void run() {
            while(true){
                synchronized (PriorityDemo.class){
                    count++;
                    if(count>10000000){
                        System.out.println("低优先级输出完成");
                        break;
                    }
                }
            }
        }
    }
    public static void main(String[] args){
        Thread high = new HightPriority();
        Thread low = new LowPriority();
        high.setPriority(Thread.MAX_PRIORITY);
        low.setPriority(Thread.MIN_PRIORITY);
        low.start();
        high.start();
    }
}

  • 结果:高优先级输出完成 低优先级输出完成 反复重复发现基本都是高优先级输出完成 低优先级输出完成 而不是低优先级输出完成 高优先级输出完成 这说明通过synchronized进行资源抢夺时,高优先级比低优先级更快。

  • 守护线程:
    • java 中有两种线程:用户线程守护线程
    • 守护线程是一种特殊的线程,是系统的守护者,在后台默默完成一些系统性的服务,比如垃圾回收线程、JIT线程就可以理解为守护线程。
    • 可以通过isDaemon() 方法来区别它们:如果返回false,则说明该线程是“用户线程”;否则就是“守护线程”。
    • 用户线程一般用户执行用户级任务,而守护线程也就是“后台线程”,一般用来执行后台任务。
    • 需要注意的是:Java虚拟机在“用户线程”都结束后会后退出。
    • JDK中的介绍:
      • When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class).
      • The Java Virtual Machine continues to execute threads until either of the following occurs:
      • The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
      • All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.
      • Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.
      • 每个线程都有一个优先级。“高优先级线程”会优先于“低优先级线程”执行。每个线程都可以被标记为一个守护进程或非守护进程。
      • 在一些运行的主线程中创建新的子线程时,子线程的优先级被设置为等于“创建它的主线程的优先级”,当且仅当“创建它的主线程是守护线程”时“子线程才会是守护线程”。
      • 当Java虚拟机启动时,通常有一个单一的非守护线程(该线程是通过main()方法启动)。JVM会一直运行直到下面的任意一个条件发生,JVM就会终止运行:
        • 调用了exit()方法,并且exit()有权限被正常执行。
        • 所有的“非守护线程”都死了(即JVM中仅仅只有“守护线程”)。
      • 每一个线程都被标记为“守护线程”或“用户线程”。当只有守护线程运行时,JVM会自动退出。
    • 源码:private boolean daemon = false;
    • 代码示例:
package com.wwj.text;

public class DaemonDemo  {
     public static class DaemonT extends Thread{
         @Override
         public void run() {
             while(true){
                 System.out.println("我是守护线程");
                 try {
                     Thread.sleep(1000);
                 }catch (InterruptedException e){
                     e.printStackTrace();
                 }
             }
         }
     }
     public  static void main(String[] args) throws  InterruptedException{
         Thread t = new DaemonT();
         t.setDaemon(true);
         t.start();
         Thread.sleep(2000);
     }
}

  • setDaemon(true) 要在start之前,否则将会报IllegalThreadStateException异常,告诉你守护线程设置失败,但是你的程序和线程依然可以正常执行,只是被当作用户线程而已。
  • 当我们将t设置为守护线程,这样输出结果就是:
    • 我是守护线程 我是守护线程 我是守护线程:并不是一直输出,这是因为在用户线程main执行完以后,守护线程就结束了,也就不打印了。
  • 当我们将t不设置为守护线程,这样输出结果就一直输出我是守护线程


所属网站分类: 技术文章 > 博客

作者:以天使的名义

链接:http://www.javaheidong.com/blog/article/207329/e14949f4e8e92e37af1c/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

26 0
收藏该文
已收藏

评论内容:(最多支持255个字符)