ThreadLocal<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

스레드별로 저장된 데이터의 형식을 지정합니다.

상속
ThreadLocal<T>
구현

예제

다음 예제에서는 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> 인스턴스를 초기화합니다.

ThreadLocal<T>(Boolean)

ThreadLocal<T> 인스턴스를 초기화하고 모든 값을 스레드에서 액세스할 수 있는지 여부를 지정합니다.

ThreadLocal<T>(Func<T>)

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

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

지정된 valueFactory 함수를 사용하여 ThreadLocal<T> 인스턴스 및 모든 값을 스레드에서 액세스할 수 있는지를 나타내는 플래그를 초기화합니다.

속성

IsValueCreated

Value가 현재 스레드에서 초기화되었는지 여부를 가져옵니다.

Value

현재 인스턴스에 대한 이 인스턴스의 값을 가져오거나 설정합니다.

Values

이 인스턴스에 액세스한 모든 스레드가 저장한 값을 포함하는 목록을 가져옵니다.

메서드

Dispose()

ThreadLocal<T> 클래스의 현재 인스턴스에서 사용하는 모든 리소스를 해제합니다.

Dispose(Boolean)

ThreadLocal<T> 인스턴스에서 사용하는 리소스를 해제합니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

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

ThreadLocal<T> 인스턴스에서 사용하는 리소스를 해제합니다.

GetHashCode()

기본 해시 함수로 작동합니다.

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

현재 인스턴스의 Type을 가져옵니다.

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

현재 Object의 단순 복사본을 만듭니다.

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

현재 스레드에 대한 이 인스턴스의 문자열 표현을 만들고 반환합니다.

적용 대상

스레드 보안

예외를 제외하고 Dispose()모든 공용 및 보호된 멤버 ThreadLocal<T> 는 스레드로부터 안전하며 여러 스레드에서 동시에 사용될 수 있습니다. 및 IsValueCreated 속성에 Value 대해 반환되는 값은 속성에 액세스하는 스레드에만 해당됩니다.

추가 정보