java user thread and thread guard

In Java, there are usually two threads: the user thread and thread guard (also called a service thread)
by Thread.setDaemon (false) set to the user thread
through Thread.setDaemon (true) set a daemon thread
to set the thread attributes to be in before the thread is started, otherwise it will be reported IllegalThreadStateException abnormal
if you do not set the thread attributes, then the default for the user thread

 
The difference between user threads and daemon threads:
 the end user thread 1. The main thread will continue to run, JVM survival
 2. If there is no user threads are daemon threads, the JVM end (all the threads come to an end)

 
  Daemon (Daemon) is running a special process in the background. It is independent of the control terminal and periodically perform some task or some events awaiting processing. That daemon thread is not dependent on the terminal, but dependent on the system, and the system "live and die together." That Java's daemon threads look like it. When the JVM all the threads are daemon threads, JVM can be pulled out; if there is one or more non-daemon thread the JVM will not quit.


  Daemon thread is a special thread, quietly do some systematic service in the background, such as garbage collection threads , the JIT threads are daemon threads . Is the corresponding user threads , user threads can be understood as a system of worker threads, it will complete the operational procedures need to be completed. If the user thread all over, means that the program needed to complete the business operation has been completed, the system can be pulled out. So when the system only daemon, java virtual machine automatically exits .

java thread is divided into user threads and daemon threads, daemon thread attribute to true representation is a daemon thread, false representation is a user thread.

Here we look at some of the features daemon threads.

Program only when a daemon thread, the system will automatically exit

public class Demo1 {

    public static class T1 extends Thread {
        public T1(String name) {
            super(name);
        }

        @Override
        public void run() {
            System.out.println ( the this .getName () + "started," + ( the this ? .IsDaemon () "I am the guardian of the thread": "I am a user thread" ));
             the while ( to true );
        }
    }

    public static void main(String[] args) {
        T1 Tl = new new Tl ( "child thread 1" );
        t1.start();
        System.out.println ( "main thread end" );
    }
}

Running the above code, the following results:

You can see the main thread has ended, but the program can not quit because: 1 child thread is a user thread, inside there is a cycle of death, it has been in operation, not an end.

Look at the following code:

public  class Demo2 {

    public static class T1 extends Thread {
        public T1(String name) {
            super(name);
        }

        @Override
        public void run() {
            System.out.println ( the this .getName () + "started," + ( the this ? .IsDaemon () "I am the guardian of the thread": "I am a user thread" ));
             the while ( to true );
        }
    }

    public static void main(String[] args) {
        T1 Tl = new new Tl ( "child thread 1" );
        t1.setDaemon(true);
        t1.start();
        System.out.println ( "main thread end" );
    }
}

operation result:

Program can be completed normally, the code by t1.setDaemon(true);the t1 threads to daemon thread, the main thread execution after where the main method is completed, the program exits.

Conclusion: When the program is finished all the user threads, regardless of whether the end of a daemon thread, the system will automatically exit.

Daemon thread must be carried out before the start () method

import java.util.concurrent.TimeUnit;

PUBLIC  Class Demo3 {

    public static void main(String[] args) {
        Thread t1 = new Thread() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
            }
        };
        t1.start();
        t1.setDaemon(true);
    }
}

t1.setDaemon(true);Is performed at t1 after the start () method is performed will be reported abnormal results are as follows:

The default value of the thread's daemon

We look at the source code to create a thread, located init Thread class () method:

Thread parent = currentThread();
this.daemon = parent.isDaemon();

The default value is dameon parent thread daemon, that is to say, if the parent thread is a user thread, the child thread is the default user site, if the parent thread is a daemon thread, the child thread default is daemon thread.

Sample code:

import java.util.concurrent.TimeUnit;

public class Demo4 {
    public static class T1 extends Thread {
        public T1(String name) {
            super(name);
        }

        @Override
        public void run() {
            System.out.println(this.getName() + ".daemon:" + this.isDaemon());
        }
    }

    public static void main(String[] args) throws InterruptedException {

        System.out.println(Thread.currentThread().getName() + ".daemon:" + Thread.currentThread().isDaemon());

        T1 t1 = new T1("t1");
        t1.start();

        Thread t2 = new Thread() {
            @Override
            public void run() {
                System.out.println(this.getName() + ".daemon:" + this.isDaemon());
                T1 t3 = new T1("t3");
                t3.start();
            }
        };

        t2.setName("t2");
        t2.setDaemon(true);
        t2.start();

        TimeUnit.SECONDS.sleep(2);
    }
}

运行代码,输出:

main.daemon:false
t1.daemon:false
t2.daemon:true
t3.daemon:true

t1是由主线程(main方法所在的线程)创建的,main线程是t1的父线程,所以t1.daemon为false,说明t1是用户线程。

t2线程调用了setDaemon(true);将其设为守护线程,t3是由t2创建的,所以t3默认线程类型和t2一样,t2.daemon为true。

 

守护线程适用场景

  针对于守护线程的特点,java 守护线程通常可用于开发一些为其它用户线程服务的功能。比如说心跳检测,事件监听等。Java 中最有名的守护进程当属GC(垃圾回收)

 

总结

  1. java中的线程分为用户线程守护线程
  2. 程序中的所有的用户线程结束之后,不管守护线程处于什么状态,java虚拟机都会自动退出
  3. 调用线程的实例方法setDaemon()来设置线程是否是守护线程
  4. setDaemon()方法必须在线程的start()方法之前调用,在后面调用会报异常,并且不起效
  5. 线程的daemon默认值和其父线程一样

 

Guess you like

Origin www.cnblogs.com/myseries/p/12078413.html