1
votes

Question: If you spawn new threads inside a servlet/controller and the main thread handling that controller has completed, will the controller thread block until all of its child threads have returned before it also returns?

Background: I have some code running in separate threads spawned from inside a controller but the thread handling the controller doesn't seem to return independently; theres a long delay of the exact length of the child thread's process time. It appears to be waiting for the child threads to finish before it exits. Is this normal?

Question Simplified: I know the child thread(s) will run without blocking the parent when the parent is running but what happens when the parent finishes its tasks?

1
Threads are completely independent in Java. - Sotirios Delimanolis
@SotiriosDelimanolis Is that always the case for servlet containers though? Its definitely waiting on the children before returning. The time delay is the same as that of its threads - Usman Mutawakil
@UsmanMutawakil It is true for all threads in Java, period. There is no way a servlet container could change these semantics, even if it wanted to. - user207421

1 Answers

2
votes

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.