Java 什么是线程上下文?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18005717/
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-11 21:43:33  来源:igfitidea点击:

What is threading context?

c#javamultithreadingsynchronization

提问by William Morrison

Does a thread's context refer to a thread's personal memory? If so, how is memory shared between multiple threads?

线程的上下文是否指的是线程的个人记忆?如果是这样,如何在多个线程之间共享内存?

I'm not looking for code examples- I understand synchronization on a high level, I'm just confused about this term, and looking to gain some insight on what's actually happening behind scenes.

我不是在寻找代码示例 - 我在高层次上理解同步,我只是对这个术语感到困惑,并希望深入了解幕后实际发生的事情。

The reason I thought/think each thread has some kind of private memory was because of the volatile keyword in Java and .NET, and how different threads can have different values for the same primitive if its not used. That always implied private memory to me.

我认为/认为每个线程都有某种私有内存的原因是因为 Java 和 .NET 中的 volatile 关键字,以及如果不使用相同的原语,不同的线程可以具有不同的值。这对我来说总是暗示着私人记忆。

As I didn't realize the term was more general, I guess I'm asking how context-switching works in Java and C# specifically.

由于我没有意识到这个术语更笼统,我想我是在问上下文切换在 Java 和 C# 中是如何工作的。

采纳答案by Eric Lippert

The reason I thought/think each thread has some kind of private memory was because of the volatile keyword in Java and .NET, and how different threads can have different values for the same primitive if its not used. That always implied private memory to me.

我认为/认为每个线程都有某种私有内存的原因是因为 Java 和 .NET 中的 volatile 关键字,以及如果不使用相同的原语,不同的线程可以具有不同的值。这对我来说总是暗示着私人记忆。

OK, now we're getting to the source of your confusion. This is one of the most confusing parts about modern programming. You have to wrap your head around this contradiction:

好的,现在我们要了解您困惑的根源了。这是现代编程中最令人困惑的部分之一。你必须解决这个矛盾:

  • All threads in a process share the same virtual memory address space, but
  • Any two threads can disagree at any time on the contents of that space
  • 一个进程中的所有线程共享相同的虚拟内存地址空间,但
  • 任何两个线程都可以随时对该空间的内容产生分歧

How can that be? Because

怎么可能?因为

  • processors make local copies of memory pages for performance reasons, and only infrequently compare notes to make sure that all their copies say the same thing. If two threads are on two different processors then they can have completely inconsistent views of "the same" memory.

  • memory in single-threaded scenarios is typically thought of as "still" unless something causes it to change. This intuition serves you poorly in multithreaded processes. If there are multiple threads accessing memory you are best to treat all memory as constantly in a state of fluxunless something is forcing it to remain still. Once you start thinking of all memory as changing all the time it becomes clear that two threads can have an inconsistent view. No two movies of the ocean during a storm are alike, even if its the same storm.

  • compilers are free to make any optimization to code that would be invisible on a single threaded system. On a multi-threaded system, those optimizations can suddenly become visible, which can lead to inconsistent views of data.

  • 处理器出于性能原因制作内存页的本地副本,并且很少比较笔记以确保它们的所有副本都说同样的话。如果两个线程在两个不同的处理器上,那么它们对“相同”内存的看法可能完全不一致。

  • 单线程场景中的内存通常被认为是“静止的”,除非某些原因导致它发生变化。 这种直觉在多线程进程中效果不佳。如果有多个线程访问内存,您最好将所有内存视为始终处于不断变化的状态,除非某些东西迫使它保持静止。一旦您开始将所有内存视为一直在变化,就很明显两个线程可能具有不一致的视图。没有两部风暴期间的海洋电影是相同的,即使是同一场风暴。

  • 编译器可以自由地对在单线程系统上不可见的代码进行任何优化。在多线程系统上,这些优化可能会突然变得可见,从而导致数据视图不一致。

If any of that is not clear, then start by reading my article explaining what "volatile" means in C#:

如果其中任何一个不清楚,那么请先阅读我的文章,该文章解释了 C# 中“易失性”的含义:

http://blogs.msdn.com/b/ericlippert/archive/2011/06/16/atomicity-volatility-and-immutability-are-different-part-three.aspx

http://blogs.msdn.com/b/ericlippert/archive/2011/06/16/atomicity-volatility-and-immutability-are-different-part-three.aspx

And then read the section "The Need For Memory Models" in Vance's article here:

然后在这里阅读 Vance 文章中的“The Need For Memory Models”部分:

http://msdn.microsoft.com/en-us/magazine/cc163715.aspx

http://msdn.microsoft.com/en-us/magazine/cc163715.aspx

Now, as for the specific question as to whether a thread has its own block of memory, the answer is yes, in two ways. First, since a thread is a point of control, and since the stack is the reification of control flow, every thread has its own million-byte stack. That's why threads are so expensive. In .NET, those million bytes are actually committed to the page fileevery time you create a thread, so be careful about creating unnecessary threads.

现在,对于线程是否有自己的内存块的具体问题,答案是肯定的,有两种方式。首先,由于线程是一个控制点,栈是控制流的具体化,每个线程都有自己的百万字节栈。这就是线程如此昂贵的原因。在 .NET 中,每次创建线程时,这些百万字节实际上都会提交到页面文件中,因此要小心创建不必要的线程。

Second, threads have the aptly named "thread local storage", which is a small section of memory associated with each thread that the thread can use to store interesting information. In C# you use the ThreadStaticattribute to mark a field as being local to a thread.

其次,线程具有恰当命名的“线程本地存储”,这是与每个线程相关联的一小部分内存,线程可以使用它来存储有趣的信息。在 C# 中,您可以使用该ThreadStatic属性将字段标记为线程的本地字段。

回答by Simon Whitehead

The actual make up of a "thread context" is implementation specific, but generally I have always understood a thread's context to refer to the current state of the thread and how it views memory at a specific time. This is what "context switching" is.. saving and restoring the state of a thread (it's context).

“线程上下文”的实际组成是特定于实现的,但通常我一直理解线程的上下文是指线程的当前状态以及它在特定时间查看内存的方式。这就是“上下文切换”的含义。保存和恢复线程的状态(它的上下文)。

Memory is shared between the contexts.. they are part of the same process.

内存在上下文之间共享......它们是同一进程的一部分。

I don't consider myself a huge expert on the topic.. but this is what I have always understood that specific term to mean.

我不认为自己是该主题的专家……但这就是我一直理解的特定术语的含义。