user471059
user471059

Reputation:

Terminate Main Thread without ensuring the termination of threads spawned by it

I have implemented a multi-threaded program which involves spawning a thread for each user,and performing some minor activities(No exhaustive processes like database connection involved).The main thread runs infinitely,and its termination is handled via monitoring file creation activity. My question is, is it okay to terminate the main thread straightaway,without waiting for the threads to finish ? (assuming that the threads would complete on their own(!),could be a false assumption).

Upvotes: 4

Views: 547

Answers (3)

Suraj Chandran
Suraj Chandran

Reputation: 24791

Yes and usually that's the case in most applicaitons. The main thread usually is repsonibly for initiating the system and it can peacefully die after that.

Note that you don't really "terminate" themain thread, instead just let it complete its run method. And thats ok.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533640

Yes, The point of threads is that they run independently.

It would only matter if your client threads were started as daemon threads and main is the only non-daemon thread. (In which case, the application would shutdown when it stops)

Upvotes: 2

Andreas Dolk
Andreas Dolk

Reputation: 114797

Sure.

The main thread is just one thread amongst others and its termination won't affect the other threads (unless you don't use System.exit() to stop the thread...).

The main thread is just the first thread*) that has been started but it has no extra or hidden features or functionalities.


*) to keep it simple - the jvm may have started some internal threads before main - but the application has no code for those threads

Upvotes: 2

Related Questions