ThreadLocal<T> 類別

定義

提供資料的執行緒區域儲存區。

C#
public class ThreadLocal<T> : IDisposable

類型參數

T

指定每個執行緒儲存的資料型別。

繼承
ThreadLocal<T>
實作

範例

下列範例顯示如何使用 ThreadLocal<T>

C#
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)

建構函式

ThreadLocal<T>()

初始化 ThreadLocal<T> 執行個體。

ThreadLocal<T>(Boolean)

初始化 ThreadLocal<T> 執行個體,並指定是否可從任何執行緒存取所有值。

ThreadLocal<T>(Func<T>)

使用指定的 valueFactory 函式來初始化 ThreadLocal<T> 的執行個體。

ThreadLocal<T>(Func<T>, Boolean)

使用指定的 valueFactory 函式及指出是否可從任何執行緒存取所有值的旗標,初始化 ThreadLocal<T> 執行個體。

屬性

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()

建立並傳回目前執行緒的這個執行個體的字串表示。

適用於

產品 版本
.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 回的值是存取屬性之執行緒的特定值。

另請參閱