Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

private static in main()

Posted on 2013-05-26
4
Medium Priority
?
289 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 1600 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

718 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