How does the program keep running all the time?

I wonder if people will think the questioner is a fool when asking this question ( and ask the bosses to spray it lightly = =). But this problem really bothers me! The cause of the
problem is this. Let"s take a look at the following code

.
public static void main(String[] args) {
    System.out.println(1);        
}

when running this code, the program starts to run-> console output 1-> the program ends, and the code stops automatically after the run is complete, that is to say, I can understand that the program has a beginning and an end. So how do you make the program not end automatically? (Java) the methods I"ve learned about are while true endless loops, or wait ()

public static void main(String[] args) {
        System.out.println(1);
        while (true) {}
    }
public static void main(String[] args) {
        Object object = new Object();
        System.out.println(1);
        synchronized (object) {
            try {
                object.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

neither of these methods will stop voluntarily, but doesn"t it feel a little too ugly? For example, web containers such as tomcat or nginx are unlikely to write such "mindless" code, and it is impossible to listen in a "mindless" loop over and over again when listening to a port, is it? So I wonder, how do they always run in the background? Of course, their specific implementation must be very complicated, I do not want to know their specific implementation (because it is too lame to)), but want to know this idea, I would like to ask the bosses here, how is this idea or which aspect do I have to start to understand this problem?.

Dec.04,2021

in fact, nginx is really an infinite loop (apache has not seen the source code, is not clear, but should also be a loop). And this is indeed a routine practice, which is basically done when doing server-side network programming.

the endless cycle you're talking about is so ugly that I really don't see it.

and it's not brainless. If you do not use the event mechanism, looping to accept (accept can block, so it will not be brainless). If a client connects to it, it will deal with it; if you use the event mechanism, it will cycle to listen to whether an event is coming (event waiting can also be blocked, so it will not be brainless), and deal with it if there is one.

Menu