Java 线程关联是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19587323/
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
What does Thread Affinity mean?
提问by Hossein
Somewhere I have heard about Thread Affinity and Thread Affinity Executor. But I cannot find a proper reference for it at least in java. Can someone please explain to me what is it all about?
我在某处听说过 Thread Affinity 和 Thread Affinity Executor。但至少在java中我找不到合适的参考。有人可以向我解释一下这是怎么回事吗?
采纳答案by Holger
There are two issues. First, it's preferable that threads have an affinity to a certain CPU (core) to make the most of their CPU-local caches. This must be handled by the operating system. This CPU affinityfor threads is often also called “thread affinity”. In case of Java, there is no standard API to get control over this. But there are 3rd party libraries, as mentioned by other answers.
有两个问题。首先,线程最好与某个 CPU(核心)有亲缘关系,以充分利用它们的 CPU 本地缓存。这必须由操作系统处理。线程的这种CPU 亲和性通常也称为“线程亲和性”。在 Java 的情况下,没有标准 API 来控制它。但是正如其他答案所提到的,有 3rd 方库。
Second, in Java there is the observation that in typical programs objects are thread-affine, i.e. typically used by only one thread most of the time. So it's the task of the JVM's optimizer to ensure, that objects affine to one thread are placed close to each other in memory to fit into one CPU's cache but place objects affine to different threads not too close to each other to avoid that they share a cache line as otherwise two CPUs/Cores have to synchronize them too often.
其次,在 Java 中观察到,在典型的程序中对象是线程仿射的,即大多数时间通常仅由一个线程使用。因此,JVM 优化器的任务是确保仿射到一个线程的对象在内存中彼此靠近放置以适合一个 CPU 的缓存,但将仿射到不同线程的对象放置在彼此之间不要太近以避免它们共享一个缓存线,否则两个 CPU/核心必须过于频繁地同步它们。
The ideal situation is that a CPU can work on some objects independently to another CPU working on other objects placed in an unrelated memory region.
理想的情况是,一个 CPU 可以独立处理某些对象,而另一个 CPU 可以处理位于不相关内存区域中的其他对象。
Practical examples of optimizations considering Thread Affinity of Java objects are
考虑 Java 对象的线程亲和性的优化的实际示例是
- Thread-Local Allocation Buffers (TLABs)
With TLABs, each object starts its lifetime in a memory region dedicated to the thread which created it. According to the main hypothesis behind generational garbage collectors (“the majority of all objects will die young”), most objects will spent their entire lifetime in such a thread local buffer.
- Biased Locking
With Biased Locking, JVMs will perform locking operations with the optimistic assumption that the object will be locked by the same thread only, switching to a more expensive locking implementation only when this assumption does not hold.
- @Contended
To address the other end, fields which are known to be accessed by multiple threads, HotSpot/OpenJDK has an annotation, currently not part of a public API, to mark them, to direct the JVM to move these data away from the other, potentially unshared data.
- 线程本地分配缓冲区 (TLAB)
使用 TLAB,每个对象在专用于创建它的线程的内存区域中开始其生命周期。根据分代垃圾收集器背后的主要假设(“所有对象中的大多数将在年轻时死去”),大多数对象将在这样的线程本地缓冲区中度过其整个生命周期。
- 偏置锁定
使用偏置锁定,JVM 将在乐观假设下执行锁定操作,即对象将仅被同一线程锁定,仅当此假设不成立时才切换到更昂贵的锁定实现。
- @有争议的
为了解决另一端,即已知可被多个线程访问的字段,HotSpot/OpenJDK 有一个注释(目前不是公共 API 的一部分)来标记它们,以指示 JVM 将这些数据移离另一个,潜在地非共享数据。
回答by PMF
Thread affinity (or process affinity) describes on which processor cores the thread/process is allowed to run. Normally, this setting is equal to the (logical) CPUs in your system, and there's hardly a reason for changing this, because the operating system then has the best possibilities to schedule your tasks among the available processors.
线程关联(或进程关联)描述了允许线程/进程在哪些处理器内核上运行。通常,此设置等于您系统中的(逻辑)CPU,并且几乎没有理由更改此设置,因为操作系统具有在可用处理器之间调度您的任务的最佳可能性。
See i.e. http://msdn.microsoft.com/en-us/library/windows/desktop/ms683213(v=vs.85).aspxfor how this works in windows. I don't know whether java offers an API to set these.
请参阅即http://msdn.microsoft.com/en-us/library/windows/desktop/ms683213(v=vs.85).aspx了解其在 Windows 中的工作原理。我不知道 java 是否提供了一个 API 来设置这些。
回答by Imran
The Java Thread Affinity version 1.4 libraryattempts to get the best of both worlds, by allowing you to reserve a logical thread for critical threads, and reserve a whole core for the most performance sensitive threads. Less critical threads will still run with the benefits of hyper threading. e.g. following code snippet
该Java线程亲和1.4版本库试图获得两全其美,通过允许您保留一个逻辑线程的关键线程,并保留完整的内核在大多数性能敏感的线程。不太重要的线程仍将运行,并具有超线程的好处。例如以下代码片段
AffinityLock al = AffinityLock.acquireLock();
try {
// find a cpu on a different socket, otherwise a different core.
AffinityLock readerLock = al.acquireLock(DIFFERENT_SOCKET, DIFFERENT_CORE);
new Thread(new SleepRunnable(readerLock, false), "reader").start();
// find a cpu on the same core, or the same socket, or any free cpu.
AffinityLock writerLock = readerLock.acquireLock(SAME_CORE, SAME_SOCKET, ANY);
new Thread(new SleepRunnable(writerLock, false), "writer").start();
Thread.sleep(200);
} finally {
al.release();
}
// allocate a whole core to the engine so it doesn't have to compete for resources.
al = AffinityLock.acquireCore(false);
new Thread(new SleepRunnable(al, true), "engine").start();
Thread.sleep(200);
System.out.println("\nThe assignment of CPUs is\n" + AffinityLock.dumpLocks());
回答by Prateek
Let me try explaining it. With the rise of multicore processors, message passing between threads & thread pooling, scheduling has become more costlier affair. Why this has become much heavier than before, for that we need to understand the concept of "mechanical sympathy". For details you can go through a blogon it. But in crude words, when threads are distributed across different cores of a processor, when they try to exchange messages; cache miss probability is high. Now coming to your specific question, thread affinity being able to assign specific threads to a particular processor/core. Here is one of the libraryfor java that can be used for it.
让我试着解释一下。随着多核处理器的兴起,线程之间的消息传递和线程池、调度变得更加昂贵。为什么这变得比以前重了很多,为此我们需要了解“机械同情”的概念。有关详细信息,您可以浏览有关它的博客。但粗略地说,当线程分布在处理器的不同内核上时,当它们尝试交换消息时;缓存未命中概率很高。现在来解决您的具体问题,线程关联能够将特定线程分配给特定处理器/内核。这是可以用于它的java库之一。