How Does Java Makes Use of Multiple Cores

Does Java have support for multicore processors/parallel processing?

Does Java have support for multicore
processors/parallel processing?

Yes. It also has been a platform for other programming languages where the implementation added a "true multithreading" or "real threading" selling point. The G1 Garbage Collector introduced in newer releases also makes use of multi-core hardware.

Java Concurrency in Practice

Try to get a copy of the Java Concurrency in Practice book.


If I can make use of multiple cores in
Java, what class/technique would I
use?

java.util.concurrent

Utility classes commonly useful in
concurrent programming. This package
includes a few small standardized
extensible frameworks, as well as some
classes that provide useful
functionality and are otherwise
tedious or difficult to implement.
Here are brief descriptions of the
main components.

Executors

Executor is a simple standardized interface for defining custom thread-like subsystems, including thread pools, asynchronous IO, and lightweight task frameworks.

Queues

The java.util.concurrent ConcurrentLinkedQueue class supplies an efficient scalable thread-safe non-blocking FIFO queue.

Timing

The TimeUnit class provides multiple granularities (including nanoseconds) for specifying and controlling time-out based operations. Most classes in the package contain operations based on time-outs in addition to indefinite waits.

Synchronizers

Four classes aid common special-purpose synchronization idioms. Semaphore is a classic concurrency tool. CountDownLatch is a very simple yet very common utility for blocking until a given number of signals, events, or conditions hold. [...]

Concurrent Collections

Besides Queues, this package supplies a few Collection implementations designed for use in multithreaded contexts: ConcurrentHashMap, CopyOnWriteArrayList, and CopyOnWriteArraySet.


This also comes in handy if you want to match the number of threads to the number of available CPUs for example:

int n = Runtime.getRuntime().availableProcessors();

Multiple Cores and java

The problem is that it is still very hard to understand how many threads, cores, ... are actually available.

My personal suggestion: there are several articles on the java specialist newsletter which do a very deep dive into this subject.

For example this one: http://www.javaspecialists.eu/archive/Issue135.html

or a very new, on "the number of available processors": http://www.javaspecialists.eu/archive/Issue220.html

what does jvm do to use multicore cpu resource?

The threads of one process can be distributed across different CPUs and CPU cores, just as individual processes can. Just because an application has one process does not mean that its many threads are bound to one CPU/core.

Java and multiple cores

  1. Yes. Actually, all JVMs, nowadays, use native threads. So the thread scheduling and distribution across cores is done by the OS, not by the JVM.

  2. Scala executes on the JVM, just as Java. So there is no difference in how threads use cores between Java and Scala

  3. There is no reason to have one JVM per core. You could want that to avoid using two much memory per JVM, or to be able to go for version 1 to version 2 without having any service interruption, but that is a different matter.

  4. No reason linked to multithreading problems. As I said, you might want to have two JVMs to be able to shutdown one without having any service interruption, or to isolate two versions of the same app for different customers for example. But that has nothing to do with multithreading.

Why Is Java Not Utilising All My CPU Cores Effectively

The Core i5 in a Lenovo X1 Carbon is not a quad core processor. It's a two core processor with hyperthreading. When you're performing only trivial operations that do not result in frequent, long pipeline stalls, then the hyperthreading scheduler won't have much opportunity to weave other operations into the stalled pipeline and you won't see performance equivalent to four actual cores.

Java 8 automatically using multicore?

Java 8 does not automatically distribute the work on all CPU cores, unless your code requests it explicitly (for example by using parallel streams).

In some special cases the Hotspot compiler will auto-vectorize the code, see for example JDK-6340864. However, automatic vectorization is using special SIMD CPU instructions, not multiple CPUs.

Also see these answers:

  • Does the JVM have the ability to detect opportunities for parallelization?
  • Automatic parallelization

(Note that I rewrote the answer, removing the part which was corrected by the comments)

How to use multiple cores with java?

Your program has a sequential bottleneck which is printing to the terminal.

System.out.println has a synchronized block in it and hence writes are one at a time, and hence your code is not parallel.

Sequential parts (including coordination) of the program is what governs its performance according to Amdahl's law



Related Topics



Leave a reply



Submit