Parent and child threads in Java

Source: Internet
Author: User
Tags jboss server

In the past when learning the operating system, the memory of the parent thread has died, the child thread has disappeared. However, today in the search for information, found a little doubt, in this record.

Java-written programs are run in a Java Virtual machine (JVM), and within the JVM, the program's multitasking is done through threads.

Starting a Java application with Java commands starts a JVM process. In the same JVM process, there is only one process, which is itself. In this JVM environment, all program code runs as threads. The JVM finds the entry point of the program, Main (), and then runs the main () method, which creates a thread called the main thread. When the Main method finishes (no other threads), the main thread runs complete. The JVM process also exits.

The operating system manages the process threads, and in turn (without a fixed order) allocates a short period of time for each process (not necessarily evenly distributed), and then within each process, the program code processes the internal threads of the process itself, switching between threads to execute, and the switching time is very short.

For a program, if the main process exits when the child process is not finished, the Linux kernel changes the parent process ID of the child process to 1 (that is, the init process), and the child process is reclaimed by the Init process when the child process is finished.

What happens if we change the process to a thread? Suppose the main thread exits before the end of the child thread, what happens to the child threads?

First, let's look at an example of a lot of people online:

Package Test;public class Test1 extends thread{@Overridepublic void Run () {while (true) {Try{thread.sleep (2000);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} System.out.println ("I'm still Alive");}} public static void Main (string[] args) throws Interruptedexception{thread t = new Test1 (); T.start (); Thread.Sleep (5000); System.out.println ("Main End");}}



Output:

I'm still alive, and I'm alive. Main end I'm still alive.



That's what I said.The JVM finds the entry point of the program, Main (), and then runs the main () method, which creates a thread called the main thread. When the Main method finishes (no other threads), the main thread runs complete. The JVM process also exits. However, the above output indicates that when main () runs to the end, the child threads are still output. So everyone came to the conclusion that the parent thread would wait for the child thread to complete before exiting. However, let's look at an example:

Package Test;public class Test extends thread{@Overridepublic void Run () {Thread sonthread = new A (); Sonthread.start ();} public static void Main (string[] args) throws Interruptedexception{thread Fatherthread = new Test (); Fatherthread.start () ; Thread.Sleep (Fatherthread.interrupt); Thread.Sleep (2000); System.out.println ("Fatherthread.isalive ()?  " +fatherthread.isalive ());}} Class A extends thread{@Overridepublic void Run () {while (true) {try{thread.sleep (1000);} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("I'm still Alive");}}}



Output:

I'm still alive, I'm alive, I'm alive, I'm alive, I'm alive, I'm living. Fatherthread.isalive ()?  I'm still alive, I'm alive, I'm still alive.



Obviously, the child thread is still outputting after the parent thread dies. Two examples which one is right?

A lot of information has been found to be answered.

If no other thread is created in the main method, the JVM ends the Java application when the main method returns. But if other threads are created in the main method, then the JVM will switch between the main thread and the other threads to ensure that each thread has the opportunity to use the CPU resources, and theMain method returns (the main thread ends) the JVM will not end , Wait until all threads of the program end before the Java program ends (another scenario: the Exit method for the runtime class is called in the program, and the security Manager allows the exit operation to occur.) The JVM will also end the program).

So again, how does the JVM know that threads are all over?

There is one thread DESTROYJAVAVM in the JVM, and the thread that executes main () invokes the JNI_DESTROYJAVAVM () method in JNI after main executes to invoke the DESTROYJAVAVM thread. After the JBoss server is started, the JVM evokes the DESTROYJAVAVM thread, waits, waits for other threads (Java threads and native threads) to notify it to unload the JVM when it exits. when a thread exits, it will determine whether it is currently the last non-deamon thread in the entire JVM , and if so, notifies the DESTROYJAVAVM thread to unload the JVM. PS: Extension: 1. If the thread exits with the judgment that it is not the last non-Deamon thread, call Thread->exit (false) and throw the Thread_end event in it, and the JVM does not exit. 2. If the thread exits with the last non-Deamon thread, call the Before_exit () method and throw two events: Event 1:thread_end thread End event, event 2:VM death event. Then call the Thread->exit (True) method, then remove the thread from the active list, remove the thread, and so on, and then notify the waiting DESTROYJAVAVM thread to perform the unload JVM operation.

So the first example is that the main thread runs out, but it is not the last non-daemon thread, so the JVM does not exit, so the child thread will continue to run.

A second example. The main thread is always there, so the JVM does not exit. When the parent thread dies, the child thread is still running. indicates that the life cycle of the parent thread is not related to a child thread.

Resources:

1. http://warnerhit.iteye.com/blog/1407484

2. http://liyuanlife.com/blog/2015/04/08/influence-of-main-threads-exiting-to-child-thread/

3. http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp47

4. http://jinguo.iteye.com/blog/747256

Parent and child threads in Java

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.