In a highly concurrent program with lots of shutdown operations, wondering how to set the exit code without prematurely calling System.exit()? Possible to set an "execute this code when everything else is done" method? but I'd really just like to prematurely set the exit code.
-
@SJuan76 - and do what with it?djechlin– djechlin2012-09-07 20:45:07 +00:00Commented Sep 7, 2012 at 20:45
-
From your question it just looked like you wanted to setup/store the exit value before exiting the thread. The static variable was to store the value and to retrieve it when you did the exit. Seeing that you were looking for shutdown hooks, now I know it was not the idea.SJuan76– SJuan762012-09-07 21:34:00 +00:00Commented Sep 7, 2012 at 21:34
4 Answers
If I understand correctly what you want is to somehow keep the exit code, run some methods and then call System.exit with the pre-decided exit code.
IMO what you should do is use Shutdown hooks instead. I.e. your code will run before the JVM shuts down and (if I got your requirement correctly) will have the same result with a straightforward coding implementation (i.e. instead of using using state variable and unusual coding logic to achieve what you are trying to do etc)
4 Comments
System.exit?Just call System.exit with the status code you are interested in and let it shutdown properlyJVM starts running the shutdown hooks.So all your code that you need on shutting down should be placed there.Call System.exit in the proper place (once with the code you need) and let the JVM start running the hooks in proper fashion. Your code will run before JVM exits.In a highly concurrent program with lots of shutdown operations
This is a code smell to me.
I can understand how multiple threads might want to shut down, but they shouldn't be allowed to do so.
Instead, I would create a global method called initiateShutdown(int code). This method would contain logic to determine when it's appropriate to actually shut down. Since you may not want a thread returning from this method, you could implement some sort of never-returning lock, and consign the thread to waiting on this lock.
1 Comment
Just store the result somewhere and use any suitable synchronization tool to tell that you are done. When you are done, just read the stored result and exit using System.exit(result).
I'm curious, if several threads set the result, which should you use?