Java 如何利用多核?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4436422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:28:36  来源:igfitidea点击:

How does Java makes use of multiple cores?

javamultithreadingparallel-processing

提问by Munish Sodhi

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线程来实现高并发的呢?

回答by Steve-o

Green threadswere replaced by native threads in Java 1.2.

在 Java 1.2 中,绿色线程被本地线程取代。

回答by Emil

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.

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

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

Java 7 中fork/join 框架来使用多个内核。

Related Questions:

相关问题:

回答by Vijay Mathew

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.
  • Make use of the facilities provided by the java.util.concurrentpackage.
  • Make sure that you have Java Concurrency in Practicein your personal library.
  • 在实现并行算法时,生成与内核数一样多的线程可能会更好。( Runtime.getRuntime().availableProcessors()). 不多也不少。
  • 使用java.util.concurrent套餐提供的设施。
  • 确保您的个人库中有Java Concurrency in Practice

回答by Gray

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线程来实现高并发的呢?

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.

如果在多 CPU 机器上运行,Java 将利用底层操作系统的线程来完成在不同 CPU 上执行代码的实际工作。当每个Java线程启动时,它创建了一个相关的操作系统线程和OS负责调度等。JVM的某些做一些管理和喜欢的线程和Java语言结构的跟踪volatilesynchronizednotify()wait(),等所有影响运行操作系统线程的状态。

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 volatileand synchronizedcalls 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.

但在内存方面,线程获得了很多性能提升,因为本地 per-CPU 缓存内存。当一个线程在 CPU 上运行时,CPU 的本地高速内存缓存帮助线程在本地隔离存储请求,而不必花时间读取或写入中央内存。这就是为什么volatilesynchronized调用包含内存同步构造的原因,因为当线程需要协调它们的工作或相互通信时,必须将缓存内存刷新到主内存或使其失效。