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)
Thread |
初始化 ThreadLocal<T> 執行個體。 |
Thread |
初始化 ThreadLocal<T> 執行個體,並指定是否可從任何執行緒存取所有值。 |
Thread |
使用指定的 |
Thread |
使用指定的 |
Is |
取得值,這個值表示 Value 是否已在目前執行緒中完成初始化。 |
Value |
取得或設定目前執行緒的這個執行個體的值。 |
Values |
取得清單,其中包含所有已存取這個實例的執行緒所儲存的值。 |
Dispose() |
釋放 ThreadLocal<T> 類別目前的執行個體所使用的全部資源。 |
Dispose(Boolean) |
釋放這個 ThreadLocal<T> 執行個體所使用的資源。 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
Finalize() |
釋放這個 ThreadLocal<T> 執行個體所使用的資源。 |
Get |
做為預設雜湊函式。 (繼承來源 Object) |
Get |
取得目前執行個體的 Type。 (繼承來源 Object) |
Memberwise |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
To |
建立並傳回目前執行緒的這個執行個體的字串表示。 |
產品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7 |
.NET Framework | 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
除了 Dispose() 之外,的所有公用和受保護成員 ThreadLocal<T> 都是安全線程,而且可以從多個執行緒同時使用。 針對 和 IsValueCreated 屬性所傳 Value 回的值是存取屬性之執行緒的特定值。