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

How to guard thread Daemon in java and python

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

Share

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

This article will explain in detail how to guard thread Daemon in java and python. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

1. The so-called daemon thread is the thread running in the background of the program, and the main thread of the program Main (such as the thread created when the java program starts) will not be the daemon thread. The definition of 2.Daemon thread in Java is that if only Daemon thread is running in the virtual machine, the virtual machine exits.

There may be many threads running in the virtual machine at the same time, and the process of the virtual machine ends only when all non-daemon threads end, regardless of whether the running thread is a main () thread or not. The 3.Main main thread ends (Non-daemon thread). If the other threads running at this time is daemon threads,JVM, the threads will stop and the JVM will stop.

If the other threads running at this time has Non-daemon threads, then JVM will not stop until all Non daemon threads are finished. 4. In short, JVM will not stop until all Non-daemon threads are finished and only daemon is left. Note that the Main main program is a Non-daemon thread. Public class A

Implements Runnable {

Public void run () {for (;) {System.out.println ("hello world");}} public static void main (String [] args) throws InterruptedException {/ / TODO Auto-generated method stub Thread thread = new Thread (new A ()); / / thread.setDaemon (true)

/ / set to daemon thread.start (); Thread.sleep (3000);}}

Source: http://www.fu-he.com/java/jishu_d.asp?id=3823

Background thread and setDaemon () method

Core hint: for Java programs, as long as there is a foreground thread running, the process will not end, if there is only a background thread running in a process, the process will end. The foreground thread is relative to the background thread, and the threads described above are all foreground threads. So what kind of thread is a background thread? If a thread object calls the setDaemon (true) method before starting (calling the start () method), the thread becomes a background thread. Let's take a look at a situation in which only background threads are running in the process, as follows: example: Threa... For Java programs, as long as there is a foreground thread running, the process will not end, if only the background thread in a process is running, the process will end. The foreground thread is relative to the background thread, and the threads described above are all foreground threads. So what kind of thread is a background thread? If a thread object calls the setDaemon (true) method before starting (calling the start () method), the thread becomes a background thread. Let's take a look at a situation where only background threads are running in the process, as shown below:

Example: ThreadDaemon.java

01 public class ThreadDaemon

02 {

03 public static void main (String args [])

04 {

05 ThreadTest t = new ThreadTest ()

06 Thread tt = new Thread (t)

07 tt.setDaemon (true); / / configure background operation

08 tt.start ()

09}

10}

eleven

12 class ThreadTest implements Runnable

13 {

14 public void run ()

15 {

16 while (true)

17 {

18 System.out.println (Thread.currentThread (). GetName () + "is running.")

19}

20}

21}

From the above program and running results (figure 9-4), you can see that although an infinite loop thread is created, because it is a background thread, the entire process terminates at the end of the main thread. This validates

The statement that the process ends when only the background thread is running.

The usage of source: http://blog.csdn.net/m13666368773/article/details/7245570 Thread.setDaemon, after learning to understand:

1. SetDaemon needs to be used before the start method is called

two。 Threads are divided into user threads and background (daemon) processes, and setDaemon sets threads as background processes

3. If the jvm is full of background processes, the current jvm will exit. (then, everything is gone, including background threads.)

4. After the end of the main thread

1) the user thread will continue to run

2) if there are no user threads and all background processes, then jvm ends

In addition:

The setDaemon method sets the thread of java as a daemon thread, and the call to this method must be executed before the thread starts. Jvm exits only if all threads in the current jvm are daemon threads.

If the created thread does not show a call to this method, it defaults to the user thread.

Examples are as follows:

Package com.jack.mySample

Import java.io.IOException

Public class TestThread extends Thread {

Public TestThread () {

}

/ *

* the run method of the thread, which will run at the same time as other threads

, /

Public void run () {

For (int I = 1; 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