JVM Daemon and non-daemon threads

Source: Internet
Author: User
Tags sleep static class


Http://blog.sina.com.cn/s/blog_605f5b4f0100udin.html

Java has two threads: the daemon thread daemon and the user thread.

Any thread can be a daemon thread daemon or user thread users, almost every aspect is the same, the only difference is to determine when the virtual machine leaves:

User thread: The Java Virtual machine automatically leaves after all its non-daemon threads have left.

Daemon Thread: The daemon is used to serve the user thread, and if no other user thread is running, there is no service object and there is no reason to go on.

The Setdaemon (Boolean on) method makes it easy to set the thread daemon mode, true to daemon mode, and false to user mode. The method must be called before the thread is started, and an exception is generated when the threads are called when they are run.

When you create other threads in a daemon, the newly generated threads will be the daemon thread without setting the Daemon property. The user thread is the same as this.

Use the following procedure to illustrate:

Import static java.lang.System.out;
Import static java.lang.System.in;

Import java.io.IOException;
public class TestMain4 extends thread{
Public TestMain4 () {

}

public void Run () {
for (int i=1;i<=50;i++) {
try{
Thread.Sleep (100);
Out.println ("This was in TestMain4");
}catch (Interruptedexception ex) {
Ex.printstacktrace ();
}
Out.println ("TESTMAIN4:" +i);
}
}

public static void Main (string[] args) {
TestMain4 test=new TestMain4 ();
TestMain5 test1=new testmain4.testmain5 ();
Test.setdaemon (false);//user thread, must end before exiting JVM
Test.start ();

Test1.setdaemon (TRUE);//daemon thread, no end required
Test1.start ();
System.out.println ("isdaemon=" +test.isdaemon ());
System.out.println ("isdaemon=" +test1.isdaemon ());

try {
Out.print ("The input is:" +in.read ());
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

Static class TestMain5 extends thread{
Public TestMain5 () {

}

public void Run () {
for (int i=1;i<=50;i++) {
try{
Thread.Sleep (1000);
Out.println ("This was in TestMain5");
}catch (Interruptedexception ex) {
Ex.printstacktrace ();
}
Out.println ("TestMain5:" +i);
}
}
}
}

Results:

Isdaemon=false
Isdaemon=true
This was in TestMain4
Testmain4:1
This was in TestMain4
Testmain4:2
This was in TestMain4
Testmain4:3
This was in TestMain4
Testmain4:4
This was in TestMain4
Testmain4:5
This was in TestMain4
Testmain4:6
This was in TestMain4
Testmain4:7
This was in TestMain4
Testmain4:8
This was in TestMain4
Testmain4:9
This was in TestMain5
Testmain5:1
This was in TestMain4
Testmain4:10
This was in TestMain4
Testmain4:11
This was in TestMain4
Testmain4:12
This was in TestMain4
Testmain4:13
This was in TestMain4
Testmain4:14
This was in TestMain4
Testmain4:15
This was in TestMain4
Testmain4:16
This was in TestMain4
Testmain4:17
This was in TestMain4
Testmain4:18
This was in TestMain4
Testmain4:19
This was in TestMain5
Testmain5:2
This was in TestMain4
Testmain4:20
This was in TestMain4
Testmain4:21
5this is in TestMain4
Testmain4:22
This was in TestMain4
Testmain4:23
This was in TestMain4
Testmain4:24
This was in TestMain4
Testmain4:25
This was in TestMain4
Testmain4:26
This was in TestMain4
Testmain4:27

This was in TestMain4
testmain4:28
The input is:53this was in TestMain4
testmain4:29
This is in Testmai N5
Testmain5:3
This was in TestMain4
Testmain4:30
The is in TestMain4
Testmain4:31
The IS in Test MAIN4
Testmain4:32
This was in TestMain4
Testmain4:33
The is in TestMain4
testmain4:34
TESTMAIN4
Testmain4:35
This was in TestMain4
Testmain4:36
The is in TestMain4
Testmain4:37
The IS In TestMain4
testmain4:38
The is in TestMain4
testmain4:39
The is in TestMain5
Testmain5:4
This is in TestMain4
Testmain4:40
The is in TestMain4
testmain4:41
The is in TestMain4
testmain4:42
This was in TestMain4
Testmain4:43
The is in TestMain4
testmain4:44
6this are in TestMain4
testmain4:45
This was in TestMain4
Testmain4:46
The is in TestMain4
testmain4:47

This was in TestMain4
testmain4:48
This was in TestMain4
testmain4:49
This was in TestMain5
Testmain5:5
This was in TestMain4
Testmain4:50

You can see that when the JVM exits, the non-daemon thread, the user thread TestMain4, has finished executing, and TestMain5 is the daemon thread, and only executes 5 times to exit. If the TestMain5 is changed to a non-daemon thread, then TestMain4 must wait until the TESTMAIN5 executes to exit the JVM after execution is complete.

public static void main is run in a non-daemon thread, but it is not necessarily the non-daemon thread in the JVM that can start multiple non-daemon threads in the program, and the JVM will also wait for them to exit. Therefore, the thread that is not running the main method exits and must exit the JVM.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.