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
原文地址:https://www.cnblogs.com/JuncaiF/p/11272697.html