Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Java makes use of multiple cores?

A JVM runs in a single process and threads in a JVM share the heap belonging to that process. Then how does JVM make use of multiple cores which provide multiple OS threads for high concurrency?

like image 555
Munish Sodhi Avatar asked Dec 14 '10 06:12

Munish Sodhi


People also ask

Why Java is multi-threaded?

Multithreading and Multiprocessing are used for multitasking in Java, but we prefer multithreading over multiprocessing. This is because the threads use a shared memory area which helps to save memory, and also, the content-switching between the threads is a bit faster than the process.

Does coding use multiple cores?

Multicore programming helps you create concurrent systems for deployment on multicore processor and multiprocessor systems. A multicore processor system is a single processor with multiple execution cores in one chip. By contrast, a multiprocessor system has multiple processors on the motherboard or chip.

How does a CPU use multiple cores?

A faster CPU speed typically helps you to load applications faster, while having more cores allows you to have more programs running at the same time and to switch from one program to the other with more ease.

Can Java use multiple threads?

Java is a multi-threaded programming language which means we can develop multi-threaded program using Java.


2 Answers

You can make use of multiple cores using multiple threads. But using a higher number of threads than the number of cores present in a machine can simply be a waste of resources. You can use availableProcessors() to get the number of cores.

In Java 7 there is fork/join framework to make use of multiple cores.

Related Questions:

  • Is Multi-Threaded algorithm required to make use of Multi-core processors ?
  • Threads per Processor
  • Correctly multithreaded quicksort or mergesort algo in Java?
like image 83
Emil Avatar answered Oct 09 '22 19:10

Emil


A JVM runs in a single process and threads in a JVM share the heap belonging to that process. Then how does JVM make use of multiple cores which provide multiple OS threads for high concurrency?

Java will utilize the underlying OS threads to do the actual job of executing the code on different CPUs, if running on a multi-CPU machine. When each Java thread is started, it creates an associated OS thread and the OS is responsible for scheduling, etc.. The JVM certain does some management and tracking of the thread and Java language constructs like volatile, synchronized, notify(), wait(), etc. all affect the run status of the OS thread.

A JVM runs in a single process and threads in a JVM share the heap belonging to that process.

JVM doesn't necessary "run in a single process" because even the garbage collector and other JVM code run in different threads and the OS often represents these different threads as different processes. In Linux, for example, the single process you see in the process list is often masquerading a bunch of different thread processes. This is even if you are on a single core machine.

However, you are correct that they all share the same heap space. They actually share the same entire memory space which means code, interned strings, stack space, etc..

Then how does JVM make use of multiple cores which provide multiple OS threads for high concurrency?

Threads get their performance improvements from a couple of reasons. Obviously straight concurrency often makes the program run faster. Being able to do multiple CPU tasks at the same time can (though not always) improve the throughput of the application. You are also able to isolate IO operations to a single thread meaning that other threads can be running while a thread is waiting on IO (read/write to disk/network, etc.).

But in terms of memory, threads get a lot of their performance improvements because of local per-CPU cached memory. When a thread runs on a CPU, the local high speed memory cache for the CPU helps the thread isolate storage requests locally without having to spend the time to read or write to central memory. This is why volatile and synchronized calls include memory synchronization constructs because the cache memory has to be flushed to main memory or invalidated when threads need to coordinate their work or communicate with each other.

like image 23
Gray Avatar answered Oct 09 '22 19:10

Gray