ThreadLocal<T> Osztály

Definíció

Szálalapú adattárolást biztosít.

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ípusparaméterek

T

A szálonként tárolt adatok típusát adja meg.

Öröklődés
ThreadLocal<T>
Megvalósítás

Példák

Az alábbi példa a ThreadLocal<T>használatát mutatja be:

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)

Konstruktorok

Name Description
ThreadLocal<T>()

Inicializálja a példányt ThreadLocal<T> .

ThreadLocal<T>(Boolean)

Inicializálja a ThreadLocal<T> példányt, és megadja, hogy az összes érték elérhető-e bármelyik szálból.

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

Inicializálja a ThreadLocal<T> példányt a megadott valueFactory függvénnyel és egy jelzővel, amely jelzi, hogy az összes érték elérhető-e bármelyik szálról.

ThreadLocal<T>(Func<T>)

Inicializálja a példányt ThreadLocal<T> a megadott valueFactory függvénnyel.

Tulajdonságok

Name Description
IsValueCreated

Lekérdezi, hogy az aktuális szálon van-e Value inicializálva.

Value

Lekéri vagy beállítja a példány értékét az aktuális szálhoz.

Values

Lekéri a példányhoz hozzáférő összes szál által tárolt értékeket tartalmazó listát.

Metódusok

Name Description
Dispose()

Az osztály aktuális példánya által használt összes erőforrást felszabadítja ThreadLocal<T> .

Dispose(Boolean)

Felszabadítja a példány által ThreadLocal<T> használt erőforrásokat.

Equals(Object)

Meghatározza, hogy a megadott objektum egyenlő-e az aktuális objektummal.

(Öröklődés forrása Object)
Finalize()

Felszabadítja a példány által ThreadLocal<T> használt erőforrásokat.

GetHashCode()

Ez az alapértelmezett kivonatoló függvény.

(Öröklődés forrása Object)
GetType()

Lekéri az Type aktuális példányt.

(Öröklődés forrása Object)
MemberwiseClone()

Az aktuális Objectpéldány sekély másolatát hozza létre.

(Öröklődés forrása Object)
ToString()

Létrehozza és visszaadja a példány sztring-ábrázolását az aktuális szálhoz.

A következőre érvényes:

Szálbiztonság

A kivételektől eltekintve Dispose()az összes nyilvános és védett tagja ThreadLocal<T> szálbiztos, és egyidejűleg több szálból is használható. A visszaadott érték és Value a IsValueCreated tulajdonságok azon szálra vonatkoznak, amelyhez a tulajdonság hozzá van adva.

Lásd még