ThreadLocal<T> Klasa

Definicja

Zapewnia lokalny magazyn danych w wątku.

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

Parametry typu

T

Określa typ przechowywanych danych na wątek.

Dziedziczenie
ThreadLocal<T>
Implementuje

Przykłady

W poniższym przykładzie pokazano, jak używać polecenia 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)

Konstruktory

ThreadLocal<T>()

Inicjuje ThreadLocal<T> wystąpienie.

ThreadLocal<T>(Boolean)

Inicjuje ThreadLocal<T> wystąpienie i określa, czy wszystkie wartości są dostępne z dowolnego wątku.

ThreadLocal<T>(Func<T>)

Inicjuje ThreadLocal<T> wystąpienie z określoną valueFactory funkcją.

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

Inicjuje ThreadLocal<T> wystąpienie z określoną valueFactory funkcją i flagą wskazującą, czy wszystkie wartości są dostępne z dowolnego wątku.

Właściwości

IsValueCreated

Pobiera, czy Value jest inicjowany w bieżącym wątku.

Value

Pobiera lub ustawia wartość tego wystąpienia dla bieżącego wątku.

Values

Pobiera listę zawierającą wartości przechowywane przez wszystkie wątki, które miały dostęp do tego wystąpienia.

Metody

Dispose()

Zwalnia wszystkie zasoby używane przez bieżące wystąpienie klasy ThreadLocal<T>.

Dispose(Boolean)

Zwalnia zasoby używane przez to ThreadLocal<T> wystąpienie.

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
Finalize()

Zwalnia zasoby używane przez to ThreadLocal<T> wystąpienie.

GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Tworzy i zwraca reprezentację ciągu tego wystąpienia dla bieżącego wątku.

Dotyczy

Bezpieczeństwo wątkowe

Z wyjątkiem programu Dispose()wszystkie publiczne i chronione elementy członkowskie są ThreadLocal<T> bezpieczne wątkowo i mogą być używane współbieżnie z wielu wątków. Wartość zwracana dla Value właściwości i IsValueCreated jest specyficzna dla wątku, na którym uzyskuje się dostęp do właściwości.

Zobacz też