Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

will main thread exit before child threads complete execution? [duplicate]

Tags:

will main thread exit before child threads complete execution?

i read in 2 articles

http://www.cs.mtu.edu/~shene/NSF-3/e-Book/FUNDAMENTALS/thread-management.html

in the above article, In "Thread Termination" para, it states in Red " if the parent thread terminates, all of its child threads terminate as well."

http://www.roseindia.net/java/thread/overview-of-thread.shtml

in the above article, the last line in that page states "The main() method execution can finish, but the program will keep running until the all threads have complete its execution.".

i fee they are contradictory. if i am wrong, Please experts correct me.

In my program, a program with Main method calls the constructor of 2 threads . in the constructor of the respective threads, i am having the start() method .

     TestA  A = new TestA("TestA");
     TestB  B = new TestB("TestB");

     public TestA(String name) {
    System.out.println(name);
    t = new Thread(this);
    t.start();
}

i would like to know what happens, main thread terminates before child threads complete execution? if so, will the child threads anyway, continue their execution??

i tried running the program, some times all the child threads are getting executed complete even if the main thread exits. In the 2 threads , i am processing some files. in testA thread A alone, 1 file alone is not getting processed some times. but many times, all the files are getting processed and i do not have any issues.

like image 658
user1257836 Avatar asked Mar 11 '12 02:03

user1257836


People also ask

What happens to threads when main exits?

Exiting the main thread will not result in the process exiting if there are any other threads still active. According to the old-fashioned model of how processes exit, a process was in control of all its threads and could mediate the shutdown of those threads, thereby controlling the shutdown of the process.

Does main thread wait for child threads?

Lines 2-15: In this code, there are three threads. One is the main thread, and the other two are child threads t1 and t2 . The main thread creates the child threads and then waits for them to finish before continuing. This makes sure that the main thread doesn't continue until both child threads have finished.

What happens when the main thread stops running in a multi threaded program?

1: It is the thread from which other "child" threads will be spawned. 2: It must be the last thread to finish execution. When the main thread stops, your program terminates. name of the thread,it�s priority, and the name of its group.

Does child thread still execute even after if their parent thread dies or terminates?

Because if parent thread dies or terminates then child thread should also die or terminate. If you run those threads as daemon threads, when main thread finishes, your application will exit.


2 Answers

Java makes a distinction between a user thread and another type of thread known as a daemon thread. The difference between these two types of threads is that if the JVM determines that the only threads running in an application are daemon threads (i.e., there are no user threads), the Java runtime closes down the application. On the other hand, if at least one user thread is alive, the Java runtime won't terminate your application.

When your main() method initially receives control from the Java runtime, it executes in the context of a user thread. As long as the main-method thread or any other user thread remains alive, your application will continue to execute.

In your case, the threads are user threads and hence are allowed to complete before the main thread exits.

i am processing some files. in testA thread A alone, 1 file alone is not getting processed some times. but many times

The reason for the above is could be something else than thread exits. It could be file locks, synchronization issue etc.

Thread (Java SE 10 & JDK 10):

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:

  • The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
  • All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.
like image 94
Suresh Kumar Avatar answered Oct 25 '22 00:10

Suresh Kumar


The background threads will keep running, even if the MAIN thread completes.

If you want MAIN to stop them (example, when MAIN is complete), have your MAIN set a "keep running" flag variable (which you must set as "volatile"), which the threads occasionally look at. MAIN sets it to false (variable) or null (object) when MAIN wants to stop them. When it's false or null, then the thread must "return;".

This is somewhat complex to implement, there are many ways, but easiest is to make your Runnable an inner class, so that your Runnable has easy sharing of the flag.

For the best implementations, look up this technique in Java applets' start/stop routines.

like image 41
John Avatar answered Oct 24 '22 23:10

John