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

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.

Leave a Comment