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)
建構函式
| 名稱 | Description |
|---|---|
| ThreadLocal<T>() |
初始化實 ThreadLocal<T> 例。 |
| ThreadLocal<T>(Boolean) |
初始化實 ThreadLocal<T> 例,並指定所有值是否可從任一執行緒存取。 |
| ThreadLocal<T>(Func<T>, Boolean) |
初始化 ThreadLocal<T> 該實例時,使用指定的 |
| ThreadLocal<T>(Func<T>) |
初始化 ThreadLocal<T> 使用指定 |
屬性
| 名稱 | Description |
|---|---|
| IsValueCreated |
會取得 Value 是否初始化在目前執行緒上。 |
| Value |
取得或設定該實例的值,用於當前執行緒。 |
| Values |
會取得一個包含所有存取過此實例執行緒的數值的清單。 |
方法
| 名稱 | Description |
|---|---|
| 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值是針對存取該屬性的執行緒而定。