Hi everyone, my name is Mic

A fan who has been working for 3 years, encountered a thread related question during the interview.

Want me to help him figure it out.

The question is, "what is a daemon thread and what are its characteristics"

Check out the experts' answers below.

Expert:

OK

A daemon thread is a thread that provides services for user threads, and its life cycle depends on user threads.

Daemon threads only make sense if there are still user threads running in the JVM.

Otherwise, once the JVM process ends, the daemon thread also ends.

That is, the daemon thread does not prevent the JVM from exiting. But user threads will!

The creation methods of daemon threads and user threads are exactly the same, we only need to call the setDaemon method in the user thread and set it to true,

It means that this thread is a daemon thread.

Because the daemon thread has the characteristic of ending its own life, it is suitable for use in some background general service scenarios.

For example, the garbage collection thread in the JVM is a typical usage scenario.

The special feature of this scenario is that when the JVM process technology is used, the meaning of the existence of the memory recycling thread does not exist.

Therefore, the JVM process cannot be technically problematic because of the ongoing garbage collection.

But daemon threads cannot be used in thread pools or some IO tasks, because once the JVM exits, the daemon threads will also exit directly.

It may cause problems that tasks are not completed or resources are not released correctly.

The above is my understanding of the problem.

Summarize

Most of the students who have worked for a long time may not be able to answer this question.

First of all, the field of thread is less used in business development itself.

The daemon thread touches even less.

I always believe that only by accumulating enough skills can we more calmly cope with the long-term career development in the future.

Friends who like my works, remember to like, favorite and follow.

file

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source for Mic带你学架构 !
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!

跟着Mic学架构
810 声望1.1k 粉丝

《Spring Cloud Alibaba 微服务原理与实战》、《Java并发编程深度理解及实战》作者。 咕泡教育联合创始人,12年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。