• java并发:初探用户线程和守护线程


    用户线程和守护线程

    用户线程

    用户线程执行完,jvm退出。守护线程还是可以跑的

    /**
     * A <i>thread</i> is a thread of execution in a program. The Java
     * Virtual Machine allows an application to have multiple threads of
     * execution running concurrently.
     * <p>
     * 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 <code>Thread</code> 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.
     * <p>
     * When a Java Virtual Machine starts up, there is usually a single
     * non-daemon thread (which typically calls the method named
     * <code>main</code> of some designated class). The Java Virtual
     * Machine continues to execute threads until either of the following
     * occurs:
     * <ul>
     * <li>The <code>exit</code> method of class <code>Runtime</code> has been
     *     called and the security manager has permitted the exit operation
     *     to take place.
     * <li>All threads that are not daemon threads have died, either by
     *     returning from the call to the <code>run</code> method or by
     *     throwing an exception that propagates beyond the <code>run</code>
     *     method.
     * </ul>
     * /
    

    用户线程优先权

    例子

    package com.java.javabase.thread.base;
    
    import lombok.extern.slf4j.Slf4j;
    
    @Slf4j
    public class PriorityTest {
        private static  int size =10;
        public static void main(String[] args) {
            Thread t1 =new ThreadOne("t1");
            Thread t2 =new ThreadOne("t2");
            t2.setPriority(1);
            t1.start();
            t2.start();
            log.info("Thread {} prority is {}",Thread.currentThread().getName(),Thread.currentThread().getPriority());
    
    
        }
        static class ThreadOne extends  Thread{
            public ThreadOne(String name){
                super(name);
            }
    
            @Override
            public void run(){
                int i =0;
                while(i<size){
                    log.info("Thread : {} priority is {} ,run {} times",Thread.currentThread().getName(),
                            Thread.currentThread().getPriority(),i++);
                }
            }
        }
    }
    
    

    说明

    setPriority是Thread方法,用户线程的优先级是1到10,默认是5。虽然设置了优先级,但线程的执行还是在于获取cpu的执行,看操作系统的支持,
    不是你级别高,cpu就给你用的。

    守护线程

    package com.java.javabase.thread.base;
    
    import lombok.extern.slf4j.Slf4j;
    
    @Slf4j
    public class DaemonTest {
        private static  int size =10;
        public static void main(String[] args) {
            Thread t1 =new ThreadOne("t1");
            Thread t2 =new ThreadTwo("t2");
            t2.setDaemon(true);
            t1.start();
            t2.start();
            log.info("Thread {} prority is {}",Thread.currentThread().getName(),Thread.currentThread().getPriority());
    
    
        }
        static class ThreadOne extends  Thread{
                public ThreadOne(String name){
                    super(name);
                }
    
                @Override
                public void run(){
                    int i =0;
                    while(i<size){
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        log.info("Thread : {} priority is {} ,run {} times",Thread.currentThread().getName(),
                                Thread.currentThread().getPriority(),i++);
                    }
                }
        }
        static class ThreadTwo extends  Thread{
            public ThreadTwo(String name){
                super(name);
            }
    
            @Override
            public void run(){
                int i =0;
                while(true&& i<(size*10000)){
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    log.info("Thread : {} priority is {} ,run {} times",Thread.currentThread().getName(),
                            Thread.currentThread().getPriority(),i++);
                }
            }
        }
    }
    
    

    测试结果

    2019-07-30 20:49:51,963   [main] INFO  DaemonTest  - Thread main prority is 5
    2019-07-30 20:49:51,970   [t1] INFO  DaemonTest  - Thread : t1 priority is 5 ,run 0 times
    2019-07-30 20:49:51,980   [t1] INFO  DaemonTest  - Thread : t1 priority is 5 ,run 1 times
    2019-07-30 20:49:51,980   [t2] INFO  DaemonTest  - Thread : t2 priority is 5 ,run 0 times
    2019-07-30 20:49:51,991   [t1] INFO  DaemonTest  - Thread : t1 priority is 5 ,run 2 times
    2019-07-30 20:49:52,001   [t2] INFO  DaemonTest  - Thread : t2 priority is 5 ,run 1 times
    2019-07-30 20:49:52,002   [t1] INFO  DaemonTest  - Thread : t1 priority is 5 ,run 3 times
    2019-07-30 20:49:52,013   [t1] INFO  DaemonTest  - Thread : t1 priority is 5 ,run 4 times
    2019-07-30 20:49:52,021   [t2] INFO  DaemonTest  - Thread : t2 priority is 5 ,run 2 times
    2019-07-30 20:49:52,023   [t1] INFO  DaemonTest  - Thread : t1 priority is 5 ,run 5 times
    2019-07-30 20:49:52,034   [t1] INFO  DaemonTest  - Thread : t1 priority is 5 ,run 6 times
    2019-07-30 20:49:52,042   [t2] INFO  DaemonTest  - Thread : t2 priority is 5 ,run 3 times
    2019-07-30 20:49:52,045   [t1] INFO  DaemonTest  - Thread : t1 priority is 5 ,run 7 times
    2019-07-30 20:49:52,056   [t1] INFO  DaemonTest  - Thread : t1 priority is 5 ,run 8 times
    2019-07-30 20:49:52,062   [t2] INFO  DaemonTest  - Thread : t2 priority is 5 ,run 4 times
    2019-07-30 20:49:52,066   [t1] INFO  DaemonTest  - Thread : t1 priority is 5 ,run 9 times
    
  • 相关阅读:
    【Java】Caused by: com.ibatis.sqlmap.client.SqlMapException: There is no statement named *** in this SqlMap.
    【Mac】Mac 使用 zsh 后, mvn 命令无效
    【Java】Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 1099
    【Android】drawable VS mipmap
    【Android】java.lang.SecurityException: getDeviceId: Neither user 10065 nor current process has android.permission.READ_PHONE_STATE
    java sql解析
    java动态编译
    随想
    一致hash算法
    一致性哈希算法及其在分布式系统中的应用
  • 原文地址:https://www.cnblogs.com/JuncaiF/p/11272697.html
Copyright © 2020-2023  润新知