• Post Reply Bookmark Topic Watch Topic
  • New Topic

what happens when multiple "java Test ..." run on a machine ?  RSS feed

 
Steve Yu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
**************
public class Test {
private String name;
private static String bigName;
public static void main(String[] args) {
Test test = new Test();
String myName = args[0];
this.name = myName;
test.changeName(name);
}
public void changeName(String name) {
String anotherName = name + " " + name;
...
}
}
**********
For the above simple code, if I invoke several "java Test .." at the same time on a unix machine, for example if I do
java Test steve
java Test jon
java Test mark
at the same time, then will the following variables be thread safe ?
1. bigName (class var)
2. name (instance var)
3. anotherName (local var)
thanks,
 
rom chatterjee
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you need to understand is that the command 'java' starts an instance of the jvm. The jvm interprets the Test.class file handling the execution,etc.
When starting one instance of the jvm and asking it to interpret the Test.class file, the jvm loads the class file.
[LIST]
[*]It stores one copy of Test.bigName which will be available to any instances of Test running within the same jvm
[*]Each instance of Test will have access to its own instance var name
[*]For each instance of Test, the local var anotherName will only be available to the instance method changeName during the period of the method invocation
If you start many jvms and ask them all to interpret Test.class, the above will be true for each jvm. However the jvms will not have knowledge of each other. ie The static var bigName in jvm1 will not be available to instances of Test in jvm2
Hope this helps
 
Steve Yu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Steve Yu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a simple follow-up question ---- Instaeding of letting "Java Test mark", "Java Test Jon" run simultaneously, I write a script to let them run sequencially. i.e. I run "java Test Mark" followed by running "java Test jon", then are these two run in two separate JVM ? In other words, will the answers to my three original questions be changed in this scenario ?
Thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, they're still run in separate JVMs, and no, the answers don't change.
Thread safety concerns multiple threads within a single program, not multiple OS-level processes.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!