Zhiling sister has found his guardian angel, How about you?

Multithreading is a point we have been developing the most attention, because the concurrency, there will be a variety of problems, but these problems in multiple threads, but also we need to solve, have seen a little reading before content, and also share with you some knowledge about the daemon threads.

What is daemon thread

jAVA provides two threads: the user thread and thread guard, then what is the guardian of the thread it?

Daemon thread has been called "service process", "wizard thread" or "background threads" refers to provide a universal service in the background when the program running thread.

This thread does not belong to an integral part of programming. Our simple language that any daemon thread is a whole JVM in all non-daemon threads "nanny."

 

User thread and thread guard is almost the same, the only difference is that if the user has all the thread out of the running, leaving only the presence of a daemon thread, the JVM will return corresponding to quit, because when all keep non -threaded end, there is no thread needs to be guardian, the guardian no thread can continue to do the work, then that program is terminated, this time all the daemon threads will be "killed." In other words, as long as there is any non-daemon thread which is still in the running, then the guard would not terminate the thread.

But we must note that a default is not thread a daemon thread. We can use an example to prove it:

Consider the following Demo

public class DaemonThreadTest extends Thread { public static void main(String[] args) { DaemonThreadTest daemonThreadTest = new DaemonThreadTest("后台线程"); System.out.println("是否是守护线程"+"---"+daemonThreadTest.isDaemon()); } public DaemonThreadTest(String name){ super(); }}

 

The results as shown in the figure, a thread is not a daemon thread.
But in JAVA language, generally we have a daemon thread priority are relatively low, he is not only an internal JVM to provide the user in the preparation of the program can also be set up to guard their own 
thread, for example, a user thread set to guard the thread is to call setDaemom (true) object of this method before starting the thread calls start () method,
or more if the parameter is set to false when it means that the user process mode, to note that when a daemon thread produced in the other thread, then these newly generated
thread is a daemon thread by default, the user thread is true,
我们看一下代码实例
public class DaemonThreadTest2 extends Thread{    public void run(){        System.out.println(Thread.currentThread().getName()+":begain");        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(Thread.currentThread().getName()+":end");    }}class Test{    public static void main(String[] args) {        System.out.println("test1:begain");        DaemonThreadTest2 daemonThreadTest2 = new DaemonThreadTest2();        daemonThreadTest2.setDaemon(true);        daemonThreadTest2.start();        System.out.println("test1:end");    }}
大家可以简单的想一下结果是什么?

结果其实是这样的

 从运行的记过中我们也可以发现,没有输出Thread-0:end,那么为什么结果会是这种样子的呢?


其实就是在启动线程前,我们将其设置为守护线程了,当程序中只有守护线程存在的时候,JVM是可以退出的,
额就是说,当JVM中只有守护线程运行的时候,JVM会自动关闭。

因此,当test1方法在调用的结束后,main线程将会退出,这时候线程daemonThreadTest2还处于休眠状态没有运行结束,但是这个时候只有守护线程在
运行了,JVM关闭了,因此它就不会输出“Thread-0:end”了。

守护线程的一个典型的例子就是垃圾回收器,只要JVM启动,他就会一直运行,实时的区监控可以被回收的资源。
是用守护线程要注意几点:
- thread.setDaemon(true)必须在thread.start()之前设置,否则会跑出一个IllegalThreadStateException异常。你不能把正在运行的常规线程设置为守护线程。

- 在Daemon线程中产生的新线程也是Daemon的。

- 不要认为所有的应用都可以分配给Daemon来进行服务,比如读写操作或者计算逻辑。

以上就是我所介绍的守护线程,你了解了么?

关注公众号,大家可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料。

Java 极客技术公众号,是由一群热爱 Java 开发的技术人组建成立,专注分享原创、高质量的 Java 文章。如果您觉得我们的文章还不错,请帮忙赞赏、在看、转发支持,鼓励我们分享出更好的文章。

Guess you like

Origin www.cnblogs.com/justdojava/p/11271205.html