简体   繁体   English

每个JVM或每个CPU内核的线程

[英]Threads per JVM or per CPU cores

How does creating threads based on the number CPU cores per JVM differ with thread running on multiple JVMs creating the number of threads on the number of CPU cores, with the condition that all the JVMs runs on one physical system sharing the same CPU? 基于每个JVM的CPU内核数创建线程与在多个JVM上运行的线程在CPU内核数上创建线程数有何不同,并且所有JVM都在共享同一CPU的一个物理系统上运行? In other words, a multi threaded Java program running 8 threads in parallel vs the same multi-threaded program running on 8 different JVMs sharing the same CPU? 换句话说,一个并行运行8个线程的多线程Java程序与在8个共享同一CPU的不同JVM上运行的同一多线程程序相比?

I have given below some ways I found possibly to implement the parallel processing with threads, but could not understand the essential differences between them? 我在下面给出了一些发现可能用线程实现并行处理的方法,但无法理解它们之间的本质区别吗?

Approach one: A Thread queries the database changes periodically, starts (long running) threads in parallel (whenever changes occur) that works on the change data. 方法一:线程定期查询数据库更改,并并行(每次发生更改时)启动(长期运行)线程,这些线程可处理更改数据。 (The work involves arithmetical and persisting the result to a database) (这项工作涉及算术运算并将结果持久化到数据库中)

Approach two: Multiple threads queries the data changes in the database, locks the modified data, each thread starting a thread (from a thread pool) that process the change data. 方法二:多个线程查询数据库中的数据更改,锁定修改后的数据,每个线程(从线程池)启动一个线程来处理更改数据。

Approach three: Multiple Threads, essentially run from different JVMs as separate processes, queries the database, locks the changed records it has found and starts thread (from the thread pool that each on of them have, max thread on the pool being of number of CPU cores) to process the change data. 方法三:多个线程,它们实际上是作为不同的进程从不同的JVM运行,查询数据库,锁定已找到的更改记录并启动线程(从它们各自拥有的线程池中启动,该池上的最大线程数为CPU内核)以处理更改数据。

Do the third approach is in anyway better than the other two? 第三种方法是否比其他两种方法更好? If yes/no why? 如果是/否,为什么? (Because, as the monitoring threads runs on different JVMs, everyone of them can create as much threads as the CPU cores? As an example, in 8 core CPU, create 8 monitoring threads on separate JVM (as separate processes), with every one of them submitting the change jobs to a thread pool of 8? But, does not this argument fail, as there are only 8 physical cores and the processor can only run 8 threads at anytime?) (因为监视线程在不同的JVM上运行,所以每个线程都可以创建与CPU内核一样多的线程。例如,在8个核心CPU中,在单独的JVM(作为单独的进程)上创建8个监视线程,每个线程他们将变更作业提交到8个线程池中吗?但是,这个参数不会失败,因为只有8个物理内核,并且处理器在任何时候都只能运行8个线程?)

You have any other effective way to implement this scenario? 您还有其他有效的方法来实现此方案吗?

I think your answer boils down to: 我认为您的答案可以归结为:

  • Processing with one thread in one process, end of story. 在一个过程中用一个线程处理,故事结束。
  • Processing with multiple threads in one process. 在一个进程中处理多个线程。
  • Processing with multiple threads in multiple processes. 在多个进程中使用多个线程进行处理。

If your goals are to saturate the CPU with as much work as possible, and to perform your processing the fastest, then the answer is usually #2, multiple threads in one process. 如果您的目标是使CPU尽可能多地完成工作,并以最快的速度执行处理,那么答案通常是#2,一个进程中有多个线程。

Multiple threads in multiple processes doesn't buy you much, and has several downsides: 多个进程中的多个线程不会给您带来很多好处,并且有以下缺点:

  • If all threads are in the same process, then they can use slim mutexes/locks ( intra process mutexes/locks), which have significantly better performance than inter process mutexes/locks. 如果所有线程都在同一进程中,则它们可以使用苗条的互斥锁/锁(进程互斥锁/锁),其性能要比进程互斥锁/锁好得多。 Multiple processes means using kernel-provided locking primitives, which are typically much slower. 多个进程意味着使用内核提供的锁定原语,该原语通常要慢得多。

  • If all threads are in the same process, they can all access the same memory, and have all of their memory pooled together. 如果所有线程都在同一进程中,则它们都可以访问同一内存,并将所有内存池在一起。 Having everything in one heap means data colocality, and colocation can improve CPU cache performance. 将所有内容放在一个堆中意味着数据共存,并且共存可以提高CPU缓存性能。 Additionally, if you had to share data between the threads in multiple processes, you would need to use shared memory (which is cumbersome in Java) or message passing (which duplicates data, wasting CPU and ram). 此外,如果必须在多个进程的线程之间共享数据,则需要使用共享内存(在Java中很麻烦)或消息传递(重复数据,浪费CPU和内存)。

The only benefit to using multiple processes is that you can easily perform privilege separation. 使用多个进程的唯一好处是您可以轻松地执行权限分离。

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

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