If i run a same multithreaded program from multiple command prompt, what would happen? Each command prompt will run in diffrent jvm. how the shared resources will access by threads in different jvm?
If i run a same multithreaded program from multiple command prompt,
what would happen?
there is seperate JVM for each program or application. (whether application is same or different)
how the shared resources will access by threads in different jvm?
As Same multithreaded application is executed by different JVM there is nothing(resources) to share by different JVM's.
As there are different java application are running same time(multiprocessing) shared resources of machine(memory , processor etc.) are handled by OS.
Note:IF you are asking about shared resources in single multithreaded application please refer oracle docs
Assuming the JVM isn't doing voodoo magic (knowing Oracle I can't be sure) each instance of Java would be running it's own thing and allocating it's own resources. As for how the host handles threading and resource distribution is a whole nother subject with tons of discussion and no good answer on how to do it (other than "Correctly")
To share resources among different JVM you need an external player, a Redis DB or similar, however if your program is multithread, why do you need to run different process ? Why don't you run it just one time ?
Each command prompt will run in diffrent jvm - Yes.
Among different jvms Resource sharing will be decided by operating system.
If two instances of JVM run the same multi threaded application, you cannot do resource sharing. Remember, resource sharing and concurrency control is between multiple threads running a single JVM process. Two such JVM processes, running the same application code, cannot do any concurrency control and resource sharing. For example, if the first JVM process opens a file, the second JVM process will not be able to access that file.
I hope this answers your question. If there is something specific which you are trying, then describe that, so that, we can offer you suggestions.
Related
By Simply Running same code in two Different eclipse can we say that code is running in two different jvm on same system.
No. The JVM is a virtual machine the Java uses. Eclipse just runs programs on that virtual machine. It doesn't create it.
If you install two different eclipse versions and set one eclipse to run on some specific JVM as explained here
and the other to run on another JVM, then you can have them running on different JVMs on one machine. But there's no point in doing that
If you're wondering whether you can use two different JVM versions by launching them different eclipse setups, that could be possible.
If you're wondering whether they'll run in the same JVM instance, the answer is no. For example, if you intend to use a synchronized function to limit access to a shared resource like a file, it's not going to work because that will only work within the same JVM instance. If you want to isolate your applications so that one can use all available memory without impacting the other, yes, they'll be isolated by running them from different eclipse instances.
You actually don't even need to start Eclipse twice. You can run your app twice from the same Eclipse.
Is the same JVM used by all Java applications running or, does 'one JVM per Java application' apply? (say the applications are IntelliJ IDEA, a server and NetBeans for example)
Further, is there any connection between JVMs assigned and processes used by each Java application?
Generally speaking, each application will get its own JVM instance and its own OS-level process and each JVM instance is independent of each other.
There are some implementation details such as Class Data Sharing, where multiple JVM instances might share some data/memory but those have no user-visible effect to the applications (except for improved startup time, hopefully).
A common scenario however is a single application server (or "web server") such as Glassfish or Tomcat running multiple web applications. In this case, multiple web applications can share a JVM.
There's one JVM per Java application. There shouldn't be any connection between them unless you establish one, e.g. with networking. If you're working inside of an IDE, the code you write generally runs in a separate JVM. The IDE will typically connect the separate JVM for debugging. If you're dealing with multiple web applications they could share the same JVM if they're deployed to the same web container.
In theory you can run multiple applications in a JVM. In practice, they can interfere with each other in various ways. For example:
The JVM has one set of System.in/out/err, one default encoding, one default locale, one set of system properties, and so on.
If one application changes these, it affects all applications.
Any application that calls System.exit() kills all applications.
If one application thread goes wild, and consumes too much CPU or memory it will affect the other applications too.
Short answer: often, yes, you'll get one application per JVM.
Long answer: the JVM can be used that way, and that may be the best option, but it doesn't have to be.
It all depends on what you consider to be an 'application'. An IDE is a good example of an application which is presented to its end users (i.e. us) as a single entity but which is actually comprised of multiple underlying applications (compilers, test runners, static analysis tools, packagers, package managers, project / dependency management tools, etc). In that case there are a variety of tricks which the IDE uses to ensure that the user experiences an integrated experience while also being shielded (to some extent) from the individual vagaries of the underlying tools. One such trick is to do some things in a separate JVM, communicating either via text files or via the application-level debugging facilities.
Application servers (Wildfly, Glassfish, Websphere, Weblogic, etc) are applications whose raison d'etre is to act as containers for other applications to run in. In that case, from one perspective, there's a single JVM per application (i.e. one JVM is used to run the entire application server) but there are actually multiple applications contained within that JVM in their own right, each logically separated from each other in their own classloader (reducing the possibility of accidental in-process crosstalk).
So, it all really depends on what you consider an application to be. If you're purely talking about "the thing which runs when 'main()' is called", then you're looking at one application per JVM - when the OS starts the JVM, the JVM runs a single class's public static void main() method.
But once your applications start getting more complicated your boundaries become more blurred. An IDE such as Intellij or Eclipse will reuse much of the same stuff as 'javac', either in the same JVM or a different one, as well as doing different work (such as repainting the screen). And users of a web application on a (shared JVM) application server may actually be using much the same 'core' application as could be used locally via the command line.
Number of JVMs running is the number of executables invoked.
Each such application invokes its own java executable (java.exe/ javaw.exe etx for windows) which means each is running in a separate JVM.
Any application which has shared libraries will share the same copy of those libraries. Java has a fair amount of shared libraries. However, you won't notice the difference except for some memory saved.
Little late here however this info may be useful for somebody. In a Linux system, if you want to know how many JVMs are running you can try this command
$ ps -ef | grep "[j]ava" | wc -l
ps to list process, grep to search process containing "java" and wc to count lines returned
Actually this is one question that can have very confusing answers. To keep it real short:
Yes per java process, per JVM.
Runtime and ProcessBuilder follow this rule.
Loading jars using reflection and then executing the main won't spawn new JVM.
I read that each application runs in its own JVM. Why is it so ? Why don't they make one JVM run 2 or more apps ?
I read a SO post, but could not get the answers there.
Is there one JVM per Java application?
I am talking about applications launched via a public static void main(String[]) method ...)
(I assume you are talking about applications launched via a public static void main(String[]) method ...)
In theory you can run multiple applications in a JVM. In practice, they can interfere with each other in various ways. For example:
The JVM has one set of System.in/out/err, one default encoding, one default locale, one set of system properties, and so on. If one application changes these, it affects all applications.
Any application that calls System.exit() will effectively kill all applications.
If one application goes wild, and consumes too much CPU or memory it will affect the other applications too.
In short, there are lots of problems. People have tried hard to make this work, but they have never really succeeded. One example is the Echidna library, though that project has been quiet for ~10 years. JNode is another example, though they (actually we) "cheated" by hacking core Java classes (like java.lang.System) so that each application got what appeared to be independent versions of System.in/out/err, the System properties and so on1.
1 - This ("proclets") was supposed to be an interim hack, pending a proper solution using true "isolates". But isolates support stalled, primarily because the JNode architecture used a single address space with no obvious way to separate "system" and "user" stuff. So while we could create APIs that matched the isolate APIs, key isolate functionality (like cleanly killing an isolate) was virtually impossible to implement. Or at least, that was/is my view.
Reason to have one JVM pre application, basically same having OS process per application.
Here are few reasons why to have a process per application.
Application bug will not bring down / corrupt data in other applications sharing same process.
System resources are accounted per process hence per application.
Terminating process will automatically release all associated resources (application may not clean up for itself, so sharing processes may produce resource leaks).
Well some applications such a Chrome go even further creating multiple processes to isolate different tabs and plugins.
Speaking of Java there are few more reasons not to share JVM.
Heap space maintenance penalty is higher with large heap size. Multiple smaller independent heaps easier to manage.
It is fairly hard to unload "application" in JVM (there to many subtle reasons for it to stay in memory even if it is not running).
JVM have a lot of tuning option which you may want to tailor for an application.
Though there are several cases there JVM is actually shared between application:
Application servers and servlet containers (e.g. Tomcat). Server side Java specs are designed with shared server JVM and dynamic loading/unloading applications in mind.
There few attempts to create shared JVM utility for CLI applications (e.g. nailgun)
But in practice, even in server side java, it usually better to use JVM (or several) per applications, for reasons mentioned above.
For isolating execution contexts.
If one of the processes hangs, or fails, or it's security is compromised, the others don't get affected.
I think having separate runtimes also helps GC, because it has less references to handle than if it was altogether.
Besides, why would you run them all in one JVM?
Java Application Servers, like JBoss, are design to run many applications in one JVM
I have a Java program that I start like this on Windows Server 2008.
java -cp "C:\Program Files\Java\jdk1.6.0_23\jre\lib\ext\*" -server -Xms128m -Xmx512m something.something.Main config.xml
It's long running, and I want to be able to start two or more instances, and I'm wondering if there's a way to specify the CPU/Core to get better performance.
I don't know of any way to attach instances of Java programs to specific CPUs / cores. As an alternative, consider making the program concurrent, i.e. start multiple executor threads internally. This way you need to run only a single instance of it, but its individual threads would run on distinct processors/cores if there are multiple available ones.
This in fact probably yields better performance and is more scalable than running multiple processes - the Executor framework allows you to choose different thread pools, change the number of threads even dynamically, manages dead threads for you etc.
You don't need to start a new virtual machine to get better performance, you can use multi-threading to use all available cores within the same process. In fact that will perform much better that two different processes.
But if you want to start two different process, just do it; the Operating System will do the rest for you.
Check this link for more information about Java processes and threads.
Normally you should not be worried about that - your OS does the distribution of the running processes to the available cores/processors.
Does invoking java via two different command line involves two different JVMs or two separate instances of same JVM.
JVM is Java Virtual Machine, a memory space where classes (code) are loaded and objects (data) are shared. JVM is equivalent to an Operating System process.
When you type java... in you command line you are executing an independent process that loads Java classes in memory, the base classes from Java and yours (from the .class files or .jar you indicate).
Another java... command will load a different process with its own memory and will load classes by itself.
Instance word confusion: when you say 'two instances of the same JVM'. It's usual to say instance of a JVM to a separate process, it is, to a loaded independent JVM. If you are saying: two process are running JVM 1.5, OK, it's the same JVM in the sense it's the same version but they are different processes, different 'instances', independent in all sense.
Webapp confusion: A webapp (by example) is simply a bunch of classes and objects instantiated, attending some URL in a web server. You can start Tomcat with 10 different apps -it is, 10 different bunches of classes and objects each of them attending different request, but in fact they share the same memory space (OS process). A webapp cannot touch other webapp's objects because nobody gives it a reference to the other objects (and classes are in some way hidden but that's another story called: class-loading).
What is the difference in your question? I would say: two different JVM instances. :)
Each run of the the java command does invoke a new JVM instance. The running java application could run new Java Threads (like a Tomcat does with web applications).
Two separate JVMs. You can run lots of stuff inside the same JVM (say 10 webapps served up by the same Tomcat instance), but there is only 1 java command to start tomcat.
If you started Sun's java.exe from their JDK/JRE version 1.6 from the same source path twice, you would get two separate and distinct JVM instances. There would be no sharing between them unless you configured it via your applications. If you wanted two different JVM's running you would have to start a java.exe of one type (say 1.5) from one location and a java.exe (version 1.6) from another.
If you are using one JDK version to run your project for both instances, invoking java via two different command lines will involve two different instances of the same JVM
If you are using a different JDK version for each app, this will involve two different JVMs.