Thread.GetData(LocalDataStoreSlot) Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Ruft den Wert aus dem angegebenen Steckplatz im aktuellen Thread innerhalb der aktuellen Domäne des aktuellen Threads ab. Verwenden Sie für eine bessere Leistung stattdessen Felder, die mit dem ThreadStaticAttribute Attribut gekennzeichnet sind.
public:
static System::Object ^ GetData(LocalDataStoreSlot ^ slot);
public static object? GetData(LocalDataStoreSlot slot);
public static object GetData(LocalDataStoreSlot slot);
static member GetData : LocalDataStoreSlot -> obj
Public Shared Function GetData (slot As LocalDataStoreSlot) As Object
Parameter
- slot
- LocalDataStoreSlot
Der LocalDataStoreSlot Wert, aus dem der Wert abgerufen werden soll.
Gibt zurück
Der abgerufene Wert.
Beispiele
Dieser Abschnitt enthält zwei Codebeispiele. Das erste Beispiel zeigt, wie Sie ein Feld verwenden, das mit dem ThreadStaticAttribute Attribut gekennzeichnet ist, um threadspezifische Informationen zu enthalten. Das zweite Beispiel zeigt, wie Sie einen Datenplatz verwenden, um dasselbe zu tun.
Erstes Beispiel
Das folgende Beispiel zeigt, wie Sie ein Feld verwenden, das mit ThreadStaticAttribute threadspezifischen Informationen gekennzeichnet ist. Diese Technik bietet eine bessere Leistung als die im zweiten Beispiel gezeigte Technik.
using System;
using System.Threading;
class Test
{
static void Main()
{
for(int i = 0; i < 3; i++)
{
Thread newThread = new Thread(ThreadData.ThreadStaticDemo);
newThread.Start();
}
}
}
class ThreadData
{
[ThreadStatic]
static int threadSpecificData;
public static void ThreadStaticDemo()
{
// Store the managed thread id for each thread in the static
// variable.
threadSpecificData = Thread.CurrentThread.ManagedThreadId;
// Allow other threads time to execute the same code, to show
// that the static data is unique to each thread.
Thread.Sleep( 1000 );
// Display the static data.
Console.WriteLine( "Data for managed thread {0}: {1}",
Thread.CurrentThread.ManagedThreadId, threadSpecificData );
}
}
/* This code example produces output similar to the following:
Data for managed thread 4: 4
Data for managed thread 5: 5
Data for managed thread 3: 3
*/
open System
open System.Threading
type ThreadData() =
// Create a static variable to hold the data for each thread.
[<ThreadStatic; DefaultValue>]
static val mutable private threadSpecificData : int
static member ThreadStaticDemo() =
// Store the managed thread id for each thread in the static
// variable.
ThreadData.threadSpecificData <- Thread.CurrentThread.ManagedThreadId
// Allow other threads time to execute the same code, to show
// that the static data is unique to each thread.
Thread.Sleep 1000
// Display the static data.
printfn $"Data for managed thread {Thread.CurrentThread.ManagedThreadId}: {ThreadData.threadSpecificData}"
for i = 0 to 2 do
let newThread = Thread ThreadData.ThreadStaticDemo
newThread.Start()
// This code example produces output similar to the following:
// Data for managed thread 4: 4
// Data for managed thread 5: 5
// Data for managed thread 3: 3
Imports System.Threading
Class Test
<MTAThread> _
Shared Sub Main()
For i As Integer = 1 To 3
Dim newThread As New Thread(AddressOf ThreadData.ThreadStaticDemo)
newThread.Start()
Next i
End Sub
End Class
Class ThreadData
<ThreadStatic> _
Shared threadSpecificData As Integer
Shared Sub ThreadStaticDemo()
' Store the managed thread id for each thread in the static
' variable.
threadSpecificData = Thread.CurrentThread.ManagedThreadId
' Allow other threads time to execute the same code, to show
' that the static data is unique to each thread.
Thread.Sleep( 1000 )
' Display the static data.
Console.WriteLine( "Data for managed thread {0}: {1}", _
Thread.CurrentThread.ManagedThreadId, threadSpecificData )
End Sub
End Class
' This code example produces output similar to the following:
'
'Data for managed thread 4: 4
'Data for managed thread 5: 5
'Data for managed thread 3: 3
Zweites Beispiel
Im folgenden Beispiel wird veranschaulicht, wie ein Datenplatz zum Speichern threadspezifischer Informationen verwendet wird.
using System;
using System.Threading;
class Test
{
static void Main()
{
Thread[] newThreads = new Thread[4];
for(int i = 0; i < newThreads.Length; i++)
{
newThreads[i] = new Thread(
new ThreadStart(Slot.SlotTest));
newThreads[i].Start();
}
}
}
class Slot
{
static Random randomGenerator;
static LocalDataStoreSlot localSlot;
static Slot()
{
randomGenerator = new Random();
localSlot = Thread.AllocateDataSlot();
}
public static void SlotTest()
{
// Set different data in each thread's data slot.
Thread.SetData(localSlot, randomGenerator.Next(1, 200));
// Write the data from each thread's data slot.
Console.WriteLine("Data in thread_{0}'s data slot: {1,3}",
AppDomain.GetCurrentThreadId().ToString(),
Thread.GetData(localSlot).ToString());
// Allow other threads time to execute SetData to show
// that a thread's data slot is unique to the thread.
Thread.Sleep(1000);
Console.WriteLine("Data in thread_{0}'s data slot: {1,3}",
AppDomain.GetCurrentThreadId().ToString(),
Thread.GetData(localSlot).ToString());
}
}
open System
open System.Threading
module Slot =
let randomGenerator = Random()
let localSlot = Thread.AllocateDataSlot()
let slotTest () =
// Set different data in each thread's data slot.
Thread.SetData(localSlot, randomGenerator.Next(1, 200))
// Write the data from each thread's data slot.
printfn $"Data in thread_{AppDomain.GetCurrentThreadId()}'s data slot: {Thread.GetData localSlot, 3}"
// Allow other threads time to execute SetData to show
// that a thread's data slot is unique to the thread.
Thread.Sleep 1000
printfn $"Data in thread_{AppDomain.GetCurrentThreadId()}'s data slot: {Thread.GetData localSlot, 3}"
let newThreads =
[| for _ = 0 to 3 do
let thread = Thread Slot.slotTest
thread.Start()
thread |]
Imports System.Threading
Class Test
<MTAThread> _
Shared Sub Main()
Dim newThreads(3) As Thread
For i As Integer = 0 To newThreads.Length - 1
newThreads(i) = New Thread(AddressOf Slot.SlotTest)
newThreads(i).Start()
Next i
End Sub
End Class
Public Class Slot
Shared randomGenerator As Random
Shared localSlot As LocalDataStoreSlot
Shared Sub New()
randomGenerator = new Random()
localSlot = Thread.AllocateDataSlot()
End Sub
Shared Sub SlotTest()
' Set different data in each thread's data slot.
Thread.SetData(localSlot, randomGenerator.Next(1, 200))
' Write the data from each thread's data slot.
Console.WriteLine("Data in thread_{0}'s data slot: {1,3}", _
AppDomain.GetCurrentThreadId().ToString(), _
Thread.GetData(localSlot).ToString())
' Allow other threads time to execute SetData to show
' that a thread's data slot is unique to the thread.
Thread.Sleep(1000)
' Write the data from each thread's data slot.
Console.WriteLine("Data in thread_{0}'s data slot: {1,3}", _
AppDomain.GetCurrentThreadId().ToString(), _
Thread.GetData(localSlot).ToString())
End Sub
End Class
Hinweise
Important
.NET Framework bietet zwei Mechanismen für die Verwendung des lokalen Threadspeichers (TLS): threadrelative statische Felder (d. h. Felder, die mit dem Attribut ThreadStaticAttribute gekennzeichnet sind) und Datenplätze. Threadrelative statische Felder bieten eine wesentlich bessere Leistung als Datenplätze und ermöglichen die Überprüfung der Kompilierungszeit. Weitere Informationen zur Verwendung von TLS finden Sie unter Thread Local Storage: Thread-Relative Statische Felder und Datenplätze.
Threads verwenden einen lokalen Speicherspeicherspeichermechanismus, um threadspezifische Daten zu speichern. Die Common Language Runtime weist jedem Prozess beim Erstellen ein Datenspeicherarray mit mehreren Steckplätzen zu. Der Thread kann einen Datenplatz im Datenspeicher zuweisen, einen Datenwert im Steckplatz speichern und abrufen und den Platz für die Wiederverwendung freigeben, nachdem der Thread abgelaufen ist. Datenplätze sind pro Thread eindeutig. Kein anderer Thread (nicht einmal ein untergeordneter Thread) kann diese Daten abrufen.
Note
GetData ist eine Shared Methode, die immer für den derzeit ausgeführten Thread gilt, auch wenn Sie sie mit einer Variablen aufrufen, die sich auf einen anderen Thread bezieht. Um Verwirrung zu vermeiden, verwenden Sie beim Aufrufen von Shared Methoden den Klassennamen: Dim test As Object = Thread.GetData(testSlot).