[Solved] Java uses how many processors available in OS? [closed]


If any OS is multi-processor, then JVM uses all of them or one of them?

On most modern hardware architectures, the JVM will make use of all CPUs/cores made available to it by the OS.

And As processors ahve different memory context so their corresponding memory threads will also have different memory contexts, so they will not be having concurrency issues?

Uh, no? I’m not sure what you are asking here but the whole issue with concurrency is that CPUs have their own cache memory. This is why memory synchronization and locking is so important. A thread may make local changes to its cached memory or its cached memory may get invalidated because of changes made to central storage by other threads. So there certainly are “concurrency issues” with multi-processor and different memory “contexts”.


Edit:

Here is the main doubt i have: If there are 4 processes being used by JVM (P1,P2,P3,P4) then threads running under one process will face concurrency issue, Thread T1 under P1, and thread T2 under P2 will never have concurrency issue as they have different memory contexts. Am i right or wrong?

You are wrong. The JVM will schedule your 4 user threads and the other background threads (gc, finalizer, jmx, etc) on the 4 processors in contention with other processes running on the same operating system. At points in time, the JVM may be using all 4 processors or may be using no processors depending on the other processes running on the OS.

If T1 and T2 are on different processors then they can be running simultaneously. None of the threads will have concurrency issues unless they share data. If T1 makes changes to an object that T2 is also using, T2 may or may not see these changes depending on what synchronization is being used. This memory synchronization is one of the big problems with multi-threaded programming. For example:

 static Integer sharedValue = 1;
 ...
 // thread T1 changes the shared value
 sharedValue = 2;
 ...
 // thread T2 reads the shared value later
 // it is unknown whether this will print 1 or 2
 System.out.println("shared = " + sharedValue);

If T2 is swapped out and T3 runs on the same processor there may actually be less concurrency issues since they are both using the same CPU cache but you still need to worry about locking. For example, if the T2 task does a ++ which seems atomic but is actually 3 operations (get, increment, store), it might get interrupted after the increment but before the store and may overwrite T3’s increment:

 static int counter = 0;
 ...
 // thread T2
 // 3 operations: get, increment, store
 // this thread might be interrupted in the middle causing a ++ to be lost
 counter++;
 ...
 // thread T3
 // 3 operations: get, increment, store
 // this thread might be interrupted in the middle causing a ++ to be lost
 counter++;
 ...
 // later on, even if T2 and T3 have completed, you may see the counter as 1 or 2
 System.out.println("counter = " + counter);

So locking is an issue even regardless of whether your threads are running on multiple or a single processor system. See here for a better example of non-atomic operations.

solved Java uses how many processors available in OS? [closed]