ThreadLocal<T> ThreadLocal<T> ThreadLocal<T> ThreadLocal<T> Class

定義

Provides thread-local storage of data.Provides thread-local storage of data.

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

指定每個執行緒儲存的資料型別。Specifies the type of data stored per-thread.

繼承
ThreadLocal<T>ThreadLocal<T>ThreadLocal<T>ThreadLocal<T>
實作

範例

下列範例顯示如何使用 ThreadLocal<T>The following example shows how to use 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>() ThreadLocal<T>()

Initializes the ThreadLocal<T> instance.Initializes the ThreadLocal<T> instance.

ThreadLocal<T>(Boolean) ThreadLocal<T>(Boolean) ThreadLocal<T>(Boolean) ThreadLocal<T>(Boolean)

Initializes the ThreadLocal<T> instance and specifies whether all values are accessible from any thread.Initializes the ThreadLocal<T> instance and specifies whether all values are accessible from any thread.

ThreadLocal<T>(Func<T>) ThreadLocal<T>(Func<T>) ThreadLocal<T>(Func<T>) ThreadLocal<T>(Func<T>)

Initializes the ThreadLocal<T> instance with the specified valueFactory function.Initializes the ThreadLocal<T> instance with the specified valueFactory function.

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

Initializes the ThreadLocal<T> instance with the specified valueFactory function and a flag that indicates whether all values are accessible from any thread.Initializes the ThreadLocal<T> instance with the specified valueFactory function and a flag that indicates whether all values are accessible from any thread.

屬性

IsValueCreated IsValueCreated IsValueCreated IsValueCreated

Gets whether Value is initialized on the current thread.Gets whether Value is initialized on the current thread.

Value Value Value Value

Gets or sets the value of this instance for the current thread.Gets or sets the value of this instance for the current thread.

Values Values Values Values

Gets a list for all of the values currently stored by all of the threads that have accessed this instance.Gets a list for all of the values currently stored by all of the threads that have accessed this instance.

方法

Dispose() Dispose() Dispose() Dispose()

Releases all resources used by the current instance of the ThreadLocal<T> class.Releases all resources used by the current instance of the ThreadLocal<T> class.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

Releases the resources used by this ThreadLocal<T> instance.Releases the resources used by this ThreadLocal<T> instance.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Finalize() Finalize() Finalize() Finalize()

Releases the resources used by this ThreadLocal<T> instance.Releases the resources used by this ThreadLocal<T> instance.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Creates and returns a string representation of this instance for the current thread.Creates and returns a string representation of this instance for the current thread.

適用於

執行緒安全性

除了Dispose()之外, 所有的ThreadLocal<T>公用和受保護成員都是安全線程, 而且可以從多個執行緒同時使用。With the exception of Dispose(), all public and protected members of ThreadLocal<T> are thread-safe and may be used concurrently from multiple threads. 針對ValueIsValueCreated屬性傳回的值, 對於存取屬性的執行緒而言是特定的。The value returned for the Value and IsValueCreated properties is specific for the thread on which the property is accessed.

另請參閱