Solved

private static in main()

Posted on 2013-05-26
4
254 Views
Last Modified: 2013-05-29
hi guys

I have a class like this


import org.springframework.batch.core.launch.support.SystemExiter;


public class CustomerMain {
private static SystemExiter sysExiter = new JvmSystemExiter();

public static void main(String args[]) {
try {
Batchjob job = doBatchJob();
if(job == success){
//do something
}else if (job == fail)
{
int myStatus = StatusUtil.getFailedStatus();
systemExiter.exit(myStatus);
}
}catch(Exception e){
}
}
}


I feel there is a problem with defining the SystemExiter sysExiter variable static here
because once it is assigned a value  like systemExiter.exit(myStatus)  it permanently stays in the JVM until jvm terminates.
So lets say in above code first thread sets myStatus as 1
If second thread sets myStatus as 2 ,systemExiter.exit(myStatus) will still be 1.

Is my understanding correct?

Thanks.
0
Comment
Question by:royjayd
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 39198385
once it is assigned a value  like systemExiter.exit(myStatus)  it permanently stays in the JVM until jvm terminates
SystemExiter.exit(myStatus) immediately exits the JVM anyway, so there is NO chance for any other code to call this method with a different value.

So lets say in above code first thread sets myStatus as 1
If second thread sets myStatus as 2
What threads are you talking about here, there is only ONE thread, the one that is executing everything in your main() method.
0
 

Author Comment

by:royjayd
ID: 39201349
I was referring to two users invoking the main()
User1(Thread1 )  invokes main()
User2(Thread2 ) invokes main()
..
0
 
LVL 35

Accepted Solution

by:
mccarl earned 400 total points
ID: 39201408
Ok, it's very unusual to have other code invoking a "main()" method like that, hence why I assumed that there is just one thread.

Regardless, my first point above still stands, that once the first thread calls systemExiter.exit(myStatus) the JVM will be exited very soon afterwards, killing any other threads that are running.


UNLESS, you are actually talking about two users starting totally different JVM's, like two users executing "java -jar some.jar arguments" on the command line at the same time??? If this is what you meant to say, then NO, static variables are only dealt with in the context on the one JVM, ie. if you start another JVM, then the systemExiter instance will be totally different in each JVM. The 2 (or more) JVM's are totally isolated from each other!
0
 

Author Comment

by:royjayd
ID: 39201589
ok thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now