Is there any possibility to execute code even after the program exits the main() method?
-
1why don't you provide us with a concrete code example of what you'd like to accomplish? – Adam Dec 21 '11 at 13:40
-
4It doesn't make much sense to throw in all those languages. – Matteo Italia Dec 21 '11 at 13:43
-
What makes you think such a thing is possible? – karlphillip Dec 21 '11 at 13:50
-
2I agree with Matteo - this would not be a bad question if it was just for one language but including four language tags just makes it four different questions... – Chris Dec 21 '11 at 13:55
9 Answers
In C and C++, functions registered through atexit() can be called after main()
returns.
In C++, static destructors can also be called after main()
returns.

- 1
- 1

- 258,201
- 41
- 486
- 479
-
I voted you up, as I meant to post the same. But to be technical, aren't registered atexit calls executed before main returns - right before, that is? – gnometorule Dec 21 '11 at 13:51
-
1@gnometorule, how would the program know that `main()` *is about to return*? It makes more sense to wait for it to return, then act accordingly (since the runtime code is the one calling `main()` in the first place). – Frédéric Hamidi Dec 21 '11 at 13:56
-
Thanks for clarifying. It was an actual question. I visualized the exit sys call to call registered atexit functions, if any, in which case the exit call proper would return after them. – gnometorule Dec 21 '11 at 13:58
-
Ah, that's not the same thing. If `exit()` is called, `main()` never returns (since `exit()` itself does not return). I was thinking of the other case, returning from `main()` without calling `exit()`. – Frédéric Hamidi Dec 21 '11 at 14:01
-
+1 for the static dtors. Similarly, static ctors run before main is called.. – Martin James Dec 21 '11 at 15:18
Maybe some other thread can continue working after program exits main(), so I can say Yes!
public class Main extends Thread {
public static void main(String[] args) {
Thread thread = new Thread(new Main());
thread.start();
System.out.println("main is exiting");
}
@Override
public void run() {
try {
sleep(1000);
System.out.println("thread is running");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

- 4,165
- 5
- 32
- 52
-
unless it is a background thread, it which case it will terminate. – Myles McDonnell Dec 21 '11 at 13:42
-
No, _all_ threads stop when you call `exit(3)`. This is why there is `pthread_exit(3)`, which allows to terminate only one thread. – fge Dec 21 '11 at 13:42
-
Yes for C#: Finalizers of objects that go out of scope only after main() has exited will be executed afterwards.
This does come in handy sometimes, e.g. to remove lockfiles

- 64,175
- 10
- 70
- 92
C++: sure. Just put it into the destructor of a global object, and it will be run after main
returns (but keep in mind that there's no guarantee that any global object is still constructed).
(... although, the trick is that the "real" entrypoint of the executable usually is not your main
, but another function that initializes globals, calls your main and when it returns it destroys them before terminating)

- 123,740
- 17
- 206
- 299
The only possibility you have is fork()
, and exit()
in the parent. There is no other way around that. Threads won't help.

- 119,121
- 33
- 254
- 329
In C and C++, there's atexit(), which will call a function during "normal" termination.

- 36,470
- 14
- 88
- 125
The simple answer is no. But you can always start a new thread at the end of the main and it will execute even after the main has completed.

- 2,647
- 15
- 50
- 85
-
I won't, the main method will wait for the thread to stop. If it was a daemon thread it will be terminated. – Jaco Van Niekerk Dec 21 '11 at 13:43
-
I disagree. Try this: **public class TestMain{ public static void main(String[] args) { System.out.println("main start"); new Thread(new Runnable(){ public void run() { while(true){ } } }).start(); System.out.println("main end"); } }** What I can see, "main end" is beeing printed which should mean the main method has completed. – Rox Dec 21 '11 at 13:57
-
I disagree. The JVM stops just before the return on the main method, waiting for the other threads to complete. It will therefore execute all statements in the main method, but the program will not terminate yet. – Jaco Van Niekerk Dec 21 '11 at 14:39
-
-
@JacoVanNiekerk : Even if you debug the program you can see that the main thread has completed and that the other thread is the only running! So I am sorry, I still think the main method completes unless you can prove I am wrong. – Rox Dec 22 '11 at 08:26
-
In C# you can create a new Thread
and set IsBackground
to false. Main
will exit but the .NET Framework will keep your your application doing whatever it needs to be doing.
static void Main(string[] args)
{
Thread thread = new Thread(() => {
Thread.Sleep(10000);
Console.WriteLine("Thread WriteLine");
});
thread.IsBackground = false;
thread.Start();
Console.WriteLine("Main WriteLine");
}

- 1,075
- 7
- 17
No, it is not possible. When the main method exists, the program terminates.

- 4,180
- 2
- 21
- 48