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?.