Solved

private static in main()

Posted on 2013-05-26
4
260 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 23
object oriented programming comparison 5 54
java stored proc example 9 21
expectj telnet failing 5 23
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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 …

910 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

20 Experts available now in Live!

Get 1:1 Help Now