Solved

private static in main()

Posted on 2013-05-26
4
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 36

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 36

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

617 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