简体   繁体   English

Java 如何利用多核?

[英]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. JVM 在单个进程中运行,JVM 中的线程共享属于该进程的堆。 Then how does JVM make use of multiple cores which provide multiple OS threads for high concurrency?那么JVM是如何利用多核提供多个OS线程来实现高并发的呢?

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 7 there is fork/join framework to make use of multiple cores.Java 7 中fork/join 框架来使用多个内核。

Related Questions:相关问题:

A JVM runs in a single process and threads in a JVM share the heap belonging to that process. JVM 在单个进程中运行,JVM 中的线程共享属于该进程的堆。 Then how does JVM make use of multiple cores which provide multiple OS threads for high concurrency?那么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.如果在多 CPU 机器上运行,Java 将利用底层操作系统线程来执行在不同 CPU 上执行代码的实际工作。 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.当每个 Java 线程启动时,它会创建一个关联的 OS 线程,OS 负责调度等。 JVM 确实对线程和 Java 语言结构(如volatilesynchronizednotify()wait() notify()进行了一些管理和跟踪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. JVM 不需要“在单个进程中运行”,因为即使是垃圾收集器和其他 JVM 代码也运行在不同的线程中,而操作系统通常将这些不同的线程表示为不同的进程。 In Linux, for example, the single process you see in the process list is often masquerading a bunch of different thread processes.例如,在 Linux 中,您在进程列表中看到的单个进程通常伪装成一堆不同的线程进程。 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?那么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.能够同时执行多个 CPU 任务可以(虽然不总是)提高应用程序的吞吐量。 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.).您还可以将 IO 操作隔离到单个线程,这意味着其他线程可以在线程等待 IO(读/写磁盘/网络等)时运行。

But in terms of memory, threads get a lot of their performance improvements because of local per-CPU cached memory.但在内存方面,线程获得了很多性能提升,因为本地 per-CPU 缓存内存。 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.当一个线程在 CPU 上运行时,CPU 的本地高速内存缓存帮助线程在本地隔离存储请求,而不必花时间读取或写入中央内存。 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.这就是为什么volatilesynchronized调用包括内存同步构造的原因,因为当线程需要协调它们的工作或相互通信时,必须将缓存内存刷新到主内存或使其失效。

Java will benefit from multiple cores, if the OS distribute threads over the available processors.如果操作系统在可用处理器上分配线程,Java 将受益于多核。 JVM itself do not do anything special to get its threads scheduled evenly across multiple cores. JVM 本身并没有做任何特殊的事情来让它的线程在多个内核之间均匀调度。 A few things to keep in mind:需要牢记以下几点:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM