ThreadLocal<T> Kelas

Definisi

Menyediakan penyimpanan data thread-local.

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

Jenis parameter

T

Menentukan jenis data yang disimpan per utas.

Warisan
ThreadLocal<T>
Penerapan

Contoh

Contoh berikut menunjukkan cara menggunakan 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)

Konstruktor

Nama Deskripsi
ThreadLocal<T>()

Menginisialisasi instans ThreadLocal<T> .

ThreadLocal<T>(Boolean)

Menginisialisasi instans ThreadLocal<T> dan menentukan apakah semua nilai dapat diakses dari utas apa pun.

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

Menginisialisasi instans ThreadLocal<T> dengan fungsi yang ditentukan valueFactory dan bendera yang menunjukkan apakah semua nilai dapat diakses dari utas apa pun.

ThreadLocal<T>(Func<T>)

Menginisialisasi instans ThreadLocal<T> dengan fungsi yang ditentukan valueFactory .

Properti

Nama Deskripsi
IsValueCreated

Mendapatkan apakah Value diinisialisasi pada utas saat ini.

Value

Mendapatkan atau menetapkan nilai instans ini untuk utas saat ini.

Values

Mendapatkan daftar yang berisi nilai yang disimpan oleh semua utas yang telah mengakses instans ini.

Metode

Nama Deskripsi
Dispose()

Merilis semua sumber daya yang digunakan oleh instans ThreadLocal<T> kelas saat ini.

Dispose(Boolean)

Merilis sumber daya yang digunakan oleh instans ini ThreadLocal<T> .

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
Finalize()

Merilis sumber daya yang digunakan oleh instans ini ThreadLocal<T> .

GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan Type instans saat ini.

(Diperoleh dari Object)
MemberwiseClone()

Membuat salinan dangkal dari Objectsaat ini.

(Diperoleh dari Object)
ToString()

Membuat dan mengembalikan representasi string instans ini untuk utas saat ini.

Berlaku untuk

Keamanan Thread

Dengan pengecualian Dispose(), semua anggota publik dan terlindungi aman ThreadLocal<T> utas dan dapat digunakan secara bersamaan dari beberapa utas. Nilai yang dikembalikan untuk Value properti dan IsValueCreated khusus untuk utas tempat properti diakses.

Lihat juga