• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
Steve Yu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I like tacos! And this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic