Solved

private static in main()

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hashmap order 17 62
jsp error 6 70
Java class and jar 3 61
Setup Eclipse for Andriod development 2 19
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

734 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