Java如何使用多核?

时间:2021-03-07 21:03:38

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?

JVM在单个进程中运行,JVM中的线程共享属于该进程的堆。那么JVM是如何利用为高并发性提供多个OS线程的多核的呢?

4 个解决方案

#1


26  

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.

您可以使用多个线程使用多个内核。但是,使用比机器中出现的内核数量更多的线程可能只是浪费资源。您可以使用availableprocessor()来获取内核的数量。

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

在Java 7中,有fork/join框架来使用多核。

Related Questions:

相关问题:

#2


17  

Green threads were replaced by native threads in Java 1.2.

Java 1.2中的本地线程取代了绿色线程。

#3


14  

Java will benefit from multiple cores, if the OS distribute threads over the available processors. JVM itself do not do anything special to get its threads scheduled evenly across multiple cores. A few things to keep in mind:

如果操作系统将线程分配到可用的处理器上,Java将受益于多核。JVM本身并没有做任何特殊的事情来让它的线程均匀地分布在多个内核中。要记住以下几点:

  • While implementing parallel algorithms, it might be better to spawn as many threads as there are cores. (Runtime.getRuntime().availableProcessors()). Not more, not less.
  • 在实现并行算法的同时,最好尽可能多地生成线程。(Runtime.getRuntime().availableProcessors())。不是更多,而不是更少。
  • Make use of the facilities provided by the java.util.concurrent package.
  • 使用java.util提供的工具。并发包。
  • Make sure that you have Java Concurrency in Practice in your personal library.
  • 确保在您的个人库中实现了Java并发。

#4


10  

Bunch of good answers here but I thought I'd add some more details for posterity.

这里有很多很好的答案,但是我想我应该为后人添加更多的细节。

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?

JVM在单个进程中运行,JVM中的线程共享属于该进程的堆。那么JVM是如何利用为高并发性提供多个OS线程的多核的呢?

As mentioned by others, 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 a 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.

正如其他人提到的,如果在多cpu机器上运行,Java将利用底层OS的线程在不同的cpu上执行代码。当Java线程启动时,它会创建一个关联的OS线程,而OS负责调度等。JVM肯定会对线程和Java语言结构(如volatile、synchronized、notify()、wait()等)进行一些管理和跟踪,这些都会影响OS线程的运行状态。

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

JVM在单个进程中运行,JVM中的线程共享属于该进程的堆。

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.

JVM不需要“在一个进程中运行”,因为即使是垃圾收集器和其他JVM代码也在不同的线程中运行,而且操作系统经常将这些不同的线程表示为不同的进程。例如,在Linux中,您在流程列表中看到的单个进程常常伪装成一堆不同的线程进程。即使你是在一台单核机器上。

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?

那么JVM是如何利用为高并发性提供多个OS线程的多核的呢?

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.).

线程的性能改进有几个原因。显然,直接并发常常使程序运行得更快。能够同时完成多个CPU任务(尽管并不总是)可以提高应用程序的吞吐量。您还可以将IO操作隔离到单个线程,这意味着当一个线程在IO上等待时,其他线程可以运行(读/写到磁盘/网络,等等)。

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.

但是在内存方面,由于每个cpu的本地缓存内存,线程获得了很多性能改进。当线程在CPU上运行时,CPU的本地高速内存缓存可以帮助线程在本地隔离存储请求,而不必花时间对*内存进行读写。这就是为什么volatile和同步调用包含内存同步结构,因为当线程需要协调它们的工作或相互通信时,缓存内存必须刷新到主内存或失效。

#1


26  

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.

您可以使用多个线程使用多个内核。但是,使用比机器中出现的内核数量更多的线程可能只是浪费资源。您可以使用availableprocessor()来获取内核的数量。

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

在Java 7中,有fork/join框架来使用多核。

Related Questions:

相关问题:

#2


17  

Green threads were replaced by native threads in Java 1.2.

Java 1.2中的本地线程取代了绿色线程。

#3


14  

Java will benefit from multiple cores, if the OS distribute threads over the available processors. JVM itself do not do anything special to get its threads scheduled evenly across multiple cores. A few things to keep in mind:

如果操作系统将线程分配到可用的处理器上,Java将受益于多核。JVM本身并没有做任何特殊的事情来让它的线程均匀地分布在多个内核中。要记住以下几点:

  • While implementing parallel algorithms, it might be better to spawn as many threads as there are cores. (Runtime.getRuntime().availableProcessors()). Not more, not less.
  • 在实现并行算法的同时,最好尽可能多地生成线程。(Runtime.getRuntime().availableProcessors())。不是更多,而不是更少。
  • Make use of the facilities provided by the java.util.concurrent package.
  • 使用java.util提供的工具。并发包。
  • Make sure that you have Java Concurrency in Practice in your personal library.
  • 确保在您的个人库中实现了Java并发。

#4


10  

Bunch of good answers here but I thought I'd add some more details for posterity.

这里有很多很好的答案,但是我想我应该为后人添加更多的细节。

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?

JVM在单个进程中运行,JVM中的线程共享属于该进程的堆。那么JVM是如何利用为高并发性提供多个OS线程的多核的呢?

As mentioned by others, 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 a 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.

正如其他人提到的,如果在多cpu机器上运行,Java将利用底层OS的线程在不同的cpu上执行代码。当Java线程启动时,它会创建一个关联的OS线程,而OS负责调度等。JVM肯定会对线程和Java语言结构(如volatile、synchronized、notify()、wait()等)进行一些管理和跟踪,这些都会影响OS线程的运行状态。

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

JVM在单个进程中运行,JVM中的线程共享属于该进程的堆。

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.

JVM不需要“在一个进程中运行”,因为即使是垃圾收集器和其他JVM代码也在不同的线程中运行,而且操作系统经常将这些不同的线程表示为不同的进程。例如,在Linux中,您在流程列表中看到的单个进程常常伪装成一堆不同的线程进程。即使你是在一台单核机器上。

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?

那么JVM是如何利用为高并发性提供多个OS线程的多核的呢?

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.).

线程的性能改进有几个原因。显然,直接并发常常使程序运行得更快。能够同时完成多个CPU任务(尽管并不总是)可以提高应用程序的吞吐量。您还可以将IO操作隔离到单个线程,这意味着当一个线程在IO上等待时,其他线程可以运行(读/写到磁盘/网络,等等)。

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.

但是在内存方面,由于每个cpu的本地缓存内存,线程获得了很多性能改进。当线程在CPU上运行时,CPU的本地高速内存缓存可以帮助线程在本地隔离存储请求,而不必花时间对*内存进行读写。这就是为什么volatile和同步调用包含内存同步结构,因为当线程需要协调它们的工作或相互通信时,缓存内存必须刷新到主内存或失效。