Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Daemon Thread in Java concurrency

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the example analysis of daemon threads in Java concurrency, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

There are two types of threads in Java: user thread (User Thread) and daemon thread (Daemon Thread).

The so-called daemon thread refers to the thread that provides a general service in the background while the program is running. for example, the garbage collection thread is a very competent guardian, and this kind of thread is not an indispensable part of the program. Therefore, when all non-daemon threads end, the program terminates and kills all daemon threads in the process. Conversely, as long as any non-daemon thread is running, the program will not terminate.

There is almost no difference between the user thread and the daemon thread, but the difference lies in the departure of the virtual machine: if the user thread has all exited and only the daemon thread exists, the virtual machine will exit. Because there is no guardian, the guardian thread has no work to do, and there is no need to continue to run the program.

Converting a thread to a daemon thread can be done by calling the setDaemon (true) method of the Thread object. There are a few points to note when using daemon threads:

(1) thread.setDaemon (true) must be set before thread.start (), otherwise an IllegalThreadStateException exception will run out. You cannot set a running regular thread as a daemon thread.

(2) the new thread generated in the Daemon thread is also Daemon.

(3) the daemon thread should never access inherent resources, such as files or databases, because it can be interrupted at any time or even in the middle of an operation.

Code example:

Import java.util.concurrent.TimeUnit; / * daemon thread * / public class Daemons {/ * * @ param args * @ throws InterruptedException * / public static void main (String [] args) throws InterruptedException {Thread d = new Thread (new Daemon ()); d.setDaemon (true); / / d.start () must be called before starting the thread; System.out.println ("d.isDaemon () =" + d.isDaemon () + ".) TimeUnit.SECONDS.sleep (1);}} class DaemonSpawn implements Runnable {public void run () {while (true) {Thread.yield ();} class Daemon implements Runnable {private Thread [] t = new Thread [10]; public void run () {for (int item0; I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report