Leggere in inglese

Condividi tramite


ThreadLocal<T> Classe

Definizione

Consente l'archiviazione dei dati nella memoria locale dei thread.

C#
public class ThreadLocal<T> : IDisposable

Parametri di tipo

T

Specifica il tipo di dati archiviati per thread.

Ereditarietà
ThreadLocal<T>
Implementazioni

Esempio

Nell'esempio riportato di seguito viene illustrato come usare 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)

Costruttori

ThreadLocal<T>()

Inizializza l'istanza di ThreadLocal<T>.

ThreadLocal<T>(Boolean)

Inizializza l'istanza di ThreadLocal<T> e specifica se tutti i valori sono accessibili da qualsiasi thread.

ThreadLocal<T>(Func<T>)

Inizializza l'istanza di ThreadLocal<T> con la funzione valueFactory specificata.

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

Inizializza l'istanza di ThreadLocal<T> con la funzione valueFactory specificata e un flag che indica se tutti i valori sono accessibili da qualsiasi thread.

Proprietà

IsValueCreated

Ottiene un valore che indica se l'oggetto Value è inizializzato sul thread corrente.

Value

Ottiene o imposta il valore di questa istanza per il thread corrente.

Values

Ottiene un elenco contenente i valori archiviati da tutti i thread che hanno eseguito l'accesso a questa istanza.

Metodi

Dispose()

Rilascia tutte le risorse usate dall'istanza corrente della classe ThreadLocal<T>.

Dispose(Boolean)

Rilascia le risorse usate dall'istanza di ThreadLocal<T>.

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
Finalize()

Rilascia le risorse usate dall'istanza di ThreadLocal<T>.

GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Crea e restituisce una rappresentazione di stringa di questa istanza per il thread corrente.

Si applica a

Prodotto Versioni
.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

Thread safety

Ad eccezione di Dispose(), tutti i membri pubblici e protetti di ThreadLocal<T> sono thread-safe e possono essere usati simultaneamente da più thread. Il valore restituito per le Value proprietà e IsValueCreated è specifico per il thread in cui viene eseguito l'accesso alla proprietà.

Vedi anche