How to handle java streams on exit?

So, I am writing a simple server-client program for school. The client sends a command and some parameters, and the server returns a response.

The server will listen for new connections and create a stream for each new client connection. The server listener is also an independent thread, which is initiated by the main one.

The server part of Main waits for user input and exits when it receives the corresponding input.

In a way, the server listener runs in a loop that says

while(true) { ... } 

So, when main reaches the end of the program and exits, will it kill all running threads? Or will he wait for their completion?

If this is the last case, is there some method I can call that will return true if the system tries to exit?

Please keep in mind that each component is part of its own class.

+4
source share
1 answer

There are two types of threads: daemon and user. The main thread is always a user thread.

A process is supported as long as at least one user thread exists. When all user threads terminate, all daemon threads are killed and the process ends.

To set the thread daemon status, you can call setDaemon() before starting the thread.

So, when main reaches the end of the program and exits, will it kill all running threads? Or will he wait for their completion?

If it is a demon stream, it will eventually be killed. If this is a user thread, the process will be supported as long as the thread continues to run.

+10
source

All Articles