https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.thread.getnameddataslot?redirectedfrom=MSDN&view=netframework-4.7.2#System_Threading_Thread_GetNamedDataSlot_System_String_
定义
查找命名的数据槽。 为了获得更好的性能,请改用以 ThreadStaticAttribute 特性标记的字段。
C#
public static LocalDataStoreSlot GetNamedDataSlot (string name);
参数
- name
- String
本地数据槽的名称。
返回
为此线程分配的 LocalDataStoreSlot。
示例
本部分包含两个代码示例。 第一个示例演示如何使用标记有一个字段ThreadStaticAttribute属性用于保存特定于线程的信息。 第二个示例演示如何使用数据槽来执行相同的操作。
第一个示例
下面的示例演示如何使用字段标有ThreadStaticAttribute用于保存特定于线程的信息。 此方法可提供更好的性能比第二个示例所示的方法。
C#
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
*/
第二个示例
下面的示例演示如何使用的命名的数据槽来存储特定于线程的信息。
C#
using System;
using System.Threading;
class Test
{
public static void Main()
{
Thread[] newThreads = new Thread[4];
int i;
for (i = 0; i < newThreads.Length; i++)
{
newThreads[i] =
new Thread(new ThreadStart(Slot.SlotTest));
newThreads[i].Start();
}
Thread.Sleep(2000);
for (i = 0; i < newThreads.Length; i++)
{
newThreads[i].Join();
Console.WriteLine("Thread_{0} finished.",
newThreads[i].ManagedThreadId);
}
}
}
class Slot
{
private static Random randomGenerator = new Random();
public static void SlotTest()
{
// Set random data in each thread's data slot.
int slotData = randomGenerator.Next(1, 200);
int threadId = Thread.CurrentThread.ManagedThreadId;
Thread.SetData(
Thread.GetNamedDataSlot("Random"),
slotData);
// Show what was saved in the thread's data slot.
Console.WriteLine("Data stored in thread_{0}'s data slot: {1,3}",
threadId, slotData);
// Allow other threads time to execute SetData to show
// that a thread's data slot is unique to itself.
Thread.Sleep(1000);
int newSlotData =
(int)Thread.GetData(Thread.GetNamedDataSlot("Random"));
if (newSlotData == slotData)
{
Console.WriteLine("Data in thread_{0}'s data slot is still: {1,3}",
threadId, newSlotData);
}
else
{
Console.WriteLine("Data in thread_{0}'s data slot changed to: {1,3}",
threadId, newSlotData);
}
}
}
注解
重要
.NET Framework 提供了使用线程本地存储 (TLS) 的两种方式: 线程相对静态字段 (即,使用标记的字段ThreadStaticAttribute属性) 和数据槽。 线程相对静态字段提供更好的性能优于数据槽,并启用编译时类型检查。 有关使用 TLS 的详细信息,请参阅线程本地存储:线程相对静态字段和数据槽。
线程使用本地存储内存机制来存储线程特定的数据。 在创建时,公共语言运行时分配将多个数据存储阵列分区到每个进程。 线程可以将数据存储区中的数据槽的分配、 存储和检索数据的槽中值以及该线程过期后释放以供重复使用的槽。 数据槽是每个线程的唯一的。没有其他线程 (甚至不是子线程) 可以获取该数据。
如果命名约定的位置不存在,将分配新的槽。 命名的数据槽是公共的可以通过任何人进行操作。