10

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.

2
  • @SJuan76 - and do what with it? Commented 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. Commented Sep 7, 2012 at 21:34

4 Answers 4

3

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)

Sign up to request clarification or add additional context in comments.

4 Comments

Program has shutdown hooks; is it unsafe to call exit from one of them? Sounds like it would loop or be illegal or something.
@djechlin:If the code in a shutdown hook is running it means the JVM is shutting down.Why would you call System.exit?Just call System.exit with the status code you are interested in and let it shutdown properly
I see, move the shutdown method to a shutdown hook, call System.exit directly instead of the method in the shutdown hook.
Yes.The idea is that on shutdown JVM 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.
1

Have a master thread spawn off all other threads such that it only shuts down when all other threads are complete.

Comments

0

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

What do you mean by "code smell"? And yes, I do have a method that does exactly that, it initiates shutdown on all objects that need to shutdown, however those shutdowns will be interrupted if it calls System.exit at the end.
0

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?

6 Comments

what "suitable synchronization tool" tells me that I'm done? And either the first one or the last one, in this program design only one is possible currently.
Which tool depends on your app. How do you know that you should exit?
In this case, instruction from MBean.
Sorry for being unclear. I ment: How can you tell that everything else is done?
That's actually exactly the thing I don't know.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.