Do child threads exit when the parent thread terminates
There is no parent/child relationship between threads. If thread A creates thread B and then thread A terminates, then thread B will continue to execute.
The exception to this is when the main thread (that is, the thread that runs the main()
function) terminates. When this happens, the process terminates and all other threads stop.
Do parent threads wait for child threads to exit before they also exit in servlet containers?
You need to explicitly call join on Thread, to wait for it to finish, otherwise it just runs in background, not affecting your parent Thread. So you controller should not block, until your child Thread return.
Although, you might have some configurations, in your container, depending on your container, which could lead to this, but I seriously doubt that.
will main thread exit before child threads complete execution?
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 classRuntime
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 therun
method.
Is it OK that child thread terminated when main thread exit in Ruby?
When the main thread exits, the process terminates and it stops all the other running threads. To avoid that, you can call Thread#join
that waits for the other thread.
thread = Thread.new {
(1..10).each do |e|
`curl http://localhost/#{e}`
end
}
thread.join
Webservers processes most probably won't exit, but they may finish serving a request before the thread ends fetching the URL. It may be better to use a background processing tool to handle that case.
C# child thread is still working even main thread exit
If you want the child thread to abort on the termination of the parent thread, it must be set as a Background thread.
t.IsBackground = true;
t.Start(i);
Otherwise, all foreground threads run to completion before the process exits
What happens to child threads when their parent dies in GHC Haskell?
What exactly happens when the parent thread dies?
Nothing. That's actually also true for POSIX threads. Threads don't share the parent-child relationship you might know from fork
in C or similar languages. There is, however, one main thread, and its termination will usually lead to the termination of the whole program:
Note that the thread in which
main()
was originally invoked differs from this. When it returns frommain()
, the effect is as if there was an implicit call toexit()
using the return value ofmain()
as the exit status.
Does the child get any exception raised? Or is there any way at all to see from the child's perspective that the parent died? Is there anything else that happens except that the parent thread just stops running?
No. No. And no. For the same reason as with usual OS threads. You can try this pretty easily:
import Control.Concurrent (forkIO, threadDelay)
delaySeconds n = threadDelay $ n * (10^6)
main = do
forkIO $ do
forkIO $ delaySeconds 1 >> print "Pseudo child 1"
forkIO $ delaySeconds 1 >> print "Pseudo child 2"
print "Pseudo parent says goodbye"
delaySeconds 10
print "Exiting main"
The "parent" will say goodbye, and the "children" will print a second later. Remember, there is no actual parent in thread programming. There are only siblings. One of them is a little bit special, yes, but that is just how it's been specified.
Is it necessary to restructure the program and propagate the child's
ThreadId
up to the parent and explicitly kill it?
At least a little bit, since forkIO
doesn't provide this. Also, if there was a forkIOKillAutomatically
, what type should it have? And why?
Or is there any other workaround for this?
Well, you could provide the rest of your parent
as another action, and therefore use a helper:
forkRunDie :: IO () -> IO () -> IO ()
forkRunDie p s = forkIO p >>= \tid -> s >> killThread tid
The above example would then become
main = do
forkIO $ do
forkRunDie (delaySeconds 1 >> print "Pseudo child 1") $ do
forkRunDie (delaySeconds 1 >> print "Pseudo child 2") $ do
print "Pseudo parent says goodbye"
delaySeconds 10
print "Exiting main"
In this case the only output will be
"Pseudo parent says goodbye"
"Exiting main"
See also:
- Smarter termination for thread racing by Conal Elliott (provides a very similar function to
forkRunDie
withfinally
).
Related Topics
How to Do Static Linking of Libwinpthread-1.Dll in Mingw
App Does Not Run with VS 2008 Sp1 Dlls, Previous Version Works with Rtm Versions
In C++, What Does & Mean After a Function's Return Type
Gnu C++ How to Check When -Std=C++0X Is in Effect
Why am I Getting String Does Not Name a Type Error
Declare Variables at Top of Function or in Separate Scopes
Compiling Qt 4.8.X for Visual Studio 2012
Multithreaded Memory Allocators for C/C++
Why Is the Volatile Qualifier Used Through Out Std::Atomic
Thread Safe Lazy Construction of a Singleton in C++
C++11: Number of Variadic Template Function Parameters
Is There Any Alternative to Using % (Modulus) in C/C++
What Is the Default Hash Function Used in C++ Std::Unordered_Map