Complete the main topic without securing the termination of the threads generated by it.

I implemented a multi-threaded program, which includes multiplying the stream for each user and performing some minor actions (there are no comprehensive processes, such as connecting to the database). The main thread runs endlessly, and its completion is processed by creating an Event monitoring file. My question is, is it ok to finish the main thread right away without waiting for the threads to finish? (assuming that threads will execute on their own (!) may be a false assumption).

+4
source share
3 answers

Sure.

The main thread is just one thread among others, and its termination will not affect other threads (unless you use System.exit() to stop the thread ...).

The main thread is only the first thread *) that was launched, but it does not have any additional or hidden functions or functionality.


*) to keep it simple: jvm may have started some internal threads before main, but the application does not have code for these threads

+3
source

Yes, stream points are that they work independently.

It would only be important if your client threads were started as daemon threads, and main is the only thread without a daemon. (In this case, the application will be disconnected when it stops)

+3
source

And usually this is the case in most applications. The main thread is usually overwritten to start the system, and after that it can die quietly.

Note that you are not really β€œcompleting” the stream of themain, but simply allowing it to complete its launch method. And this is normal.

+1
source

All Articles