ThreadLocal<T> 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供数据的线程本地存储。
generic <typename T>
public ref class ThreadLocal : IDisposable
public class ThreadLocal<T> : IDisposable
type ThreadLocal<'T> = class
interface IDisposable
Public Class ThreadLocal(Of T)
Implements IDisposable
类型参数
- T
指定按线程存储的数据类型。
- 继承
-
ThreadLocal<T>
- 实现
示例
下面的示例演示如何使用 ThreadLocal<T>:
using System;
using System.Threading;
using System.Threading.Tasks;
class ThreadLocalDemo
{
// Demonstrates:
// ThreadLocal(T) constructor
// ThreadLocal(T).Value
// One usage of ThreadLocal(T)
static void Main()
{
// Thread-Local variable that yields a name for a thread
ThreadLocal<string> ThreadName = new ThreadLocal<string>(() =>
{
return "Thread" + Thread.CurrentThread.ManagedThreadId;
});
// Action that prints out ThreadName for the current thread
Action action = () =>
{
// If ThreadName.IsValueCreated is true, it means that we are not the
// first action to run on this thread.
bool repeat = ThreadName.IsValueCreated;
Console.WriteLine("ThreadName = {0} {1}", ThreadName.Value, repeat ? "(repeat)" : "");
};
// Launch eight of them. On 4 cores or less, you should see some repeat ThreadNames
Parallel.Invoke(action, action, action, action, action, action, action, action);
// Dispose when you are done
ThreadName.Dispose();
}
}
// This multithreading example can produce different outputs for each 'action' invocation and will vary with each run.
// Therefore, the example output will resemble but may not exactly match the following output (from a 4 core processor):
// ThreadName = Thread5
// ThreadName = Thread6
// ThreadName = Thread4
// ThreadName = Thread6 (repeat)
// ThreadName = Thread1
// ThreadName = Thread4 (repeat)
// ThreadName = Thread7
// ThreadName = Thread5 (repeat)
Imports System.Threading
Imports System.Threading.Tasks
Module ThreadLocalDemo
' Demonstrates:
' ThreadLocal(T) constructor
' ThreadLocal(T).Value
' One usage of ThreadLocal(T)
Sub Main()
' Thread-Local variable that yields a name for a thread
Dim ThreadName As New ThreadLocal(Of String)(
Function()
Return "Thread" & Thread.CurrentThread.ManagedThreadId
End Function)
' Action that prints out ThreadName for the current thread
Dim action As Action =
Sub()
' If ThreadName.IsValueCreated is true, it means that we are not the
' first action to run on this thread.
Dim repeat As Boolean = ThreadName.IsValueCreated
Console.WriteLine("ThreadName = {0} {1}", ThreadName.Value, If(repeat, "(repeat)", ""))
End Sub
' Launch eight of them. On 4 cores or less, you should see some repeat ThreadNames
Parallel.Invoke(action, action, action, action, action, action, action, action)
' Dispose when you are done
ThreadName.Dispose()
End Sub
End Module
' This multithreading example can produce different outputs for each 'action' invocation and will vary with each run.
' Therefore, the example output will resemble but may not exactly match the following output (from a 4 core processor):
' ThreadName = Thread5
' ThreadName = Thread6
' ThreadName = Thread4
' ThreadName = Thread6 (repeat)
' ThreadName = Thread1
' ThreadName = Thread4 (repeat)
' ThreadName = Thread7
' ThreadName = Thread5 (repeat)
构造函数
ThreadLocal<T>() |
初始化 ThreadLocal<T> 实例。 |
ThreadLocal<T>(Boolean) |
初始化 ThreadLocal<T> 实例并指定是否可从任意线程访问所有值。 |
ThreadLocal<T>(Func<T>) |
使用指定的 |
ThreadLocal<T>(Func<T>, Boolean) |
使用指定的 |
属性
IsValueCreated |
获取是否在当前线程上初始化 Value。 |
Value |
获取或设置当前线程的此实例的值。 |
Values |
获取一个列表,其中包含所有已访问此实例的线程存储的值。 |
方法
Dispose() |
释放 ThreadLocal<T> 类的当前实例所使用的所有资源。 |
Dispose(Boolean) |
释放该 ThreadLocal<T> 实例使用的资源。 |
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
Finalize() |
释放该 ThreadLocal<T> 实例使用的资源。 |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
创建并返回当前线程的此实例的字符串表示形式。 |
适用于
线程安全性
除此外 Dispose(),所有公共成员和受保护成员 ThreadLocal<T> 都是线程安全的,并且可以从多个线程并发使用。 为 Value 属性 IsValueCreated 返回的值特定于访问属性的线程。