1

If I write a public static void main java program, unless I put a while (true) loop, the program will run and then exit, closing my applet window. My question is when a web browser runs through rendering html content, after it has finished rendering, why doesn't it close itself? Where is the while (true) implicit in a web browser?

Victor
  • 16,609
  • 71
  • 229
  • 409
  • People need time to read the content presented by the browser. – suspectus Aug 18 '13 at 22:24
  • Both are complete differently modeled – jmj Aug 18 '13 at 22:24
  • There is no explicit `while(true)` (there are other ways to make an app wait, such as `Thread#sleep()`). The applet do will wait for the "command to end", though. In a usual program, the "policy" is different, there is no waiting for outside entities, if the program has nothing else to do, it exits. – acdcjunior Aug 18 '13 at 22:27
  • 2
    A web browser is just one instance of an event driven app. There is an "infinite" loop in the event processing function.. see http://en.wikipedia.org/wiki/Event_loop – Blorgbeard Aug 18 '13 at 22:28
  • The browser does close on termination. You haven't terminated it. 'Finished rendering' != 'termination'. – user207421 Aug 19 '13 at 00:11

2 Answers2

3

Java applications end because the call stack ends / is empty after your main() method has finished and they must jump back to the place they were called from (that's how method/functions calls are eventually implemented in assembly language). The main() method is special because it's the entry point of your whole Java program, the one that is called from the outside.

In a browser, your application code runs inside an event loop, not as part of the main browser process i.e. the one started through the main() method described above. The event loop, in an overly simplified version of a program, looks something like:

public static void main(String args[]) {
  Queue queue = ...;
  while(queue.waitForEvent()){
   queue.processNextEvent();
  }
}

So, in some sense. Yes, that implicit while(true) statement you're talking about is there, somewhere deep in each browser (and other event-loop based systems) implementation. The browser process won't close / terminate until something causes that event loop to stop running e.g. you closing the tab/window where your app is running.

Community
  • 1
  • 1
a2ndrade
  • 2,403
  • 21
  • 19
1

A program started via main() can keep running, despite reaching the end of the main method. That can happen if you create new threads, which incidentally happens with GUI programs.

AWT (and swing by extension) have an event loop handling the event queue. Other toolkits handle it more or less similarly. They usually have a method/function that enters the loop. At least some also allow taking control of the toolkit's event loop, if the application needs more complete control. Then the loop can be explicit in the application code.

So, where exactly the loop is, depends on the browser implementation.

kiheru
  • 6,588
  • 25
  • 31