ThreadLocal<T> 클래스

정의

데이터의 스레드 로컬 스토리지를 제공합니다.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>을 사용하는 방법을 보여 줍니다.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> 인스턴스를 초기화합니다.Initializes the ThreadLocal<T> instance.

ThreadLocal<T>(Boolean)

ThreadLocal<T> 인스턴스를 초기화하고 모든 값을 스레드에서 액세스할 수 있는지 여부를 지정합니다.Initializes the ThreadLocal<T> instance and specifies whether all values are accessible from any thread.

ThreadLocal<T>(Func<T>)

지정된 valueFactory 함수를 사용하여 ThreadLocal<T>의 인스턴스를 초기화합니다.Initializes the ThreadLocal<T> instance with the specified valueFactory function.

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

지정된 valueFactory 함수를 사용하여 ThreadLocal<T> 인스턴스 및 모든 값을 스레드에서 액세스할 수 있는지를 나타내는 플래그를 초기화합니다.Initializes the ThreadLocal<T> instance with the specified valueFactory function and a flag that indicates whether all values are accessible from any thread.

속성

IsValueCreated

Value가 현재 스레드에서 초기화되었는지 여부를 가져옵니다.Gets whether Value is initialized on the current thread.

Value

현재 인스턴스에 대한 이 인스턴스의 값을 가져오거나 설정합니다.Gets or sets the value of this instance for the current thread.

Values

이 인스턴스에 액세스한 모든 스레드가 현재 저장한 모든 값의 목록을 가져옵니다.Gets a list for all of the values currently stored by all of the threads that have accessed this instance.

메서드

Dispose()

ThreadLocal<T> 클래스의 현재 인스턴스에서 사용하는 모든 리소스를 해제합니다.Releases all resources used by the current instance of the ThreadLocal<T> class.

Dispose(Boolean)

ThreadLocal<T> 인스턴스에서 사용하는 리소스를 해제합니다.Releases the resources used by this ThreadLocal<T> instance.

Equals(Object)

지정한 개체와 현재 개체가 같은지 여부를 확인합니다.Determines whether the specified object is equal to the current object.

(다음에서 상속됨 Object)
Finalize()

ThreadLocal<T> 인스턴스에서 사용하는 리소스를 해제합니다.Releases the resources used by this ThreadLocal<T> instance.

GetHashCode()

기본 해시 함수로 작동합니다.Serves as the default hash function.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.Gets the Type of the current instance.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.Creates a shallow copy of the current Object.

(다음에서 상속됨 Object)
ToString()

현재 스레드에 대한 이 인스턴스의 문자열 표현을 만들고 반환합니다.Creates and returns a string representation of this instance for the current thread.

적용 대상

스레드 보안

경우는 예외 Dispose()모든 public 및 protected 멤버의 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.

추가 정보