首页 \ 问答 \ Java的ThreadLocal如何实现?(How is Java's ThreadLocal implemented under the hood?)

Java的ThreadLocal如何实现?(How is Java's ThreadLocal implemented under the hood?)

ThreadLocal如何实现? 它是否在Java中实现(使用从ThreadID到对象的一些并发映射),还是使用一些JVM钩来更有效地执行它?


How is ThreadLocal implemented? Is it implemented in Java (using some concurrent map from ThreadID to object), or does it use some JVM hook to do it more efficiently?


原文:https://stackoverflow.com/questions/1202444
更新时间:2022-07-18 20:07

最满意答案

是。 这是由C#规范保证的。 在7.18节(C#3.0规范)中:

每当表达式满足上述要求时,表达式将在编译时进行评估。 即使表达式是包含非常量结构的较大表达式的子表达式,也是如此。

(“上述要求”,包括+运算符应用于两个常数表达式。)

另见这个问题 。


Yes. This is guaranteed by the C# specification. It's in section 7.18 (of the C# 3.0 spec):

Whenever an expression fulfills the requirements listed above, the expression is evaluated at compile-time. This is true even if the expression is a sub-expression of a larger expression that contains non-constant constructs.

(The "requirements listed above" include the + operator applied to two constant expressions.)

See also this question.

相关问答

更多
  • 控制字符: (十六进制代码采用与ASCII兼容的字符编码。) \a = \x07 =警报(钟) \b = \x08 = backspace \t = \x09 =水平标签 \n = \x0A =换行符(或换行符) \v = \x0B =垂直标签 \f = \x0C =换页 \r = \x0D =回车 \e = \x1B =转义(非标准GCC扩展) 标点符号: \" =引号( '"'不需要反斜杠) \' =撇号( "'"不需要反斜杠) \? =问号(用于避免三字母) \\ =反斜杠 数字字符引用: \ +最多 ...
  • 是。 这是由C#规范保证的。 在7.18节(C#3.0规范)中: 每当表达式满足上述要求时,表达式将在编译时进行评估。 即使表达式是包含非常量结构的较大表达式的子表达式,也是如此。 (“上述要求”,包括+运算符应用于两个常数表达式。) 另见这个问题 。 Yes. This is guaranteed by the C# specification. It's in section 7.18 (of the C# 3.0 spec): Whenever an expression fulfills the r ...
  • HashSet是包含以下内容的最佳容器: var ips = new HashSet { "ip1", "ip2", "ip3", "ip4", "ip5" }; if (ips.Contains(input)) { // do stuff } 对于任何类型: var ips = new HashSet { ip1, ip2, ip3, ip4, ip5 }; if (ips.Contains(input)) { // do stuff } ...
  • 我拿了你的代码并在一个简单的控制台应用程序中编译它。 然后,我检查了IL(使用ILSpy)。 在调试或释放模式下都没有进行优化。 在这种情况下,代码可能很简单,编译器没有进行任何优化。 但是,更复杂的版本可能会产生不同的结果。 另请注意,这两个示例之间的差异很小。 在这两种情况下,运行时最终会生成四个不同的字符串对象。 在第一个示例中,对变量的赋值仅执行两次,而在第二个示例中,赋值执行三次。 但是,无论如何都会创建四个字符串。 它们如下: “ABCDEFGHIJKLMNOPQRSTUVWXYZ” “abcd ...
  • 您可以使用C#中的SqlConnectionStringBuilder类创建动态连接字符串,如下所示。 有关详细信息,请访问http://msdn.microsoft.com/en-us/library/dce36088.aspx private void Form1_Load(object sender, EventArgs e) { SqlConnectionStringBuilder connectionString = new SqlConnectionStringBuilder(); ...
  • 拆分和修剪 // include linq library like this: // using System.Linq; // then "test1, test2".Split(',').Select(o => o.Trim()); 要么 "test1, test2".Split(',').Select(o => o.Trim()).ToArray(); // returns array 和 "test1, test2".Split(',').Select(o => "\"" + o.Tr ...
  • p2来自同一条款: 字符串文字是用双引号括起来的零个或多个多字节字符的序列,如"xyz" 。 再次阅读报价,强调我的: 在转换阶段6中,由任何相邻字符序列和相同前缀字符串文字标记指定的多字节字符序列被连接成单个多字节字符序列。 您可以连接相邻的字符串文字标记和相同前缀的字符串文字标记。 (为避免任何疑问,C中没有“字符文字”; 'c'是字符常量 ,参见§6.4.4.4。) p2 from the same subclause: A character string literal is a sequence ...
  • 如果您创建新字符串,它们将不会自动放入实习池,除非您连接常量编译时,在这种情况下,编译器将创建一个字符串结果和作为JIT过程的一部分的实习生。 If you create new strings, they will not automatically be put into the intern pool, unless you concatenate constants compile-time, in which case the compiler will create one string res ...
  • StandardAnalyzer过滤掉停用词,例如“it”。 但是当你搜索时,你会将“it”当作关键字。 您可以使用带有停用词列表的StandardAnalyzer构造函数 ,并使用空集来调用它。 The StandardAnalyzer filters out the stop words, such as "it". But when you are searching you include the "it" as a keyword. You could use the StandardAnalyz ...
  • 将@放在第二个字符串前或使用String.Format() Put the @ in front of the second string literal or use String.Format()

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)