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_

定义

命名空间:
System.Threading
Assemblies:
mscorlib.dll, netstandard.dll, System.Threading.Thread.dll

查找命名的数据槽。 为了获得更好的性能,请改用以 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 的详细信息,请参阅线程本地存储:线程相对静态字段和数据槽

线程使用本地存储内存机制来存储线程特定的数据。 在创建时,公共语言运行时分配将多个数据存储阵列分区到每个进程。 线程可以将数据存储区中的数据槽的分配、 存储和检索数据的槽中值以及该线程过期后释放以供重复使用的槽。 数据槽是每个线程的唯一的。没有其他线程 (甚至不是子线程) 可以获取该数据。

如果命名约定的位置不存在,将分配新的槽。 命名的数据槽是公共的可以通过任何人进行操作。

适用于

Thread.GetNamedDataSlot(String)的更多相关文章

  1. java 多线程: Thread 并发访问-代码块同步synchronized {};String作为被锁的对象

    方法同步的弊端 方法同步的时候,如果一个方法需要线程安全控制的代码速度其实很快,但是还有其他的业务逻辑代码耗时非常长(比如网络请求),这样所有的线程就在这一块就等待着了,这样造成了极大的资源浪费如果并 ...

  2. C# - 多线程 之 Process与Thread与ThreadPool

    Process 进程类, // 提供对本地和远程进程的访问,启动/停止本地系统进程 public class Process : Component { public int Id { get; } ...

  3. c# System.Threading.Thread

    using System; using System.Threading; // Simple threading scenario: Start a static method running // ...

  4. .NETFramework:Thread

    ylbtech-.NETFramework:Thread 1.返回顶部 1. #region 程序集 mscorlib, Version=2.0.0.0, Culture=neutral, Publi ...

  5. 【C# 线程】Thread类 以及使用案例

    System.Threading.Thread类 涉及到的类和枚举 Volatile 类Interlocked 类SpinLock 类SpinWait类Barrier 类ThreadLocal< ...

  6. 多线程爬坑之路-Thread和Runable源码解析

    多线程:(百度百科借一波定义) 多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提 ...

  7. Android笔记——Handler Runnable与Thread的区别

    在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run() ...

  8. Android线程管理之Thread使用总结

    前言 最近在一直准备总结一下Android上的线程管理,今天先来总结一下Thread使用. 线程管理相关文章地址: Android线程管理之Thread使用总结 Android线程管理之Executo ...

  9. Java中Runnable和Thread的区别

    在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run() ...

随机推荐

  1. strcmp()比较函数和strcasecmp()和strnatcmp()

    strcmp()的函数原型如下() int strcmp(string str1,string str2) 该函数需要两个进行比较的参数字符串,如果这两个字符串相等,该函数就返回0,如果按字典顺序st ...

  2. Vue02 样式的动态绑定

    daigengxin......2018-3-8 21:09:18 跟angular2类似,分为CSS类绑定和Style样式绑定两种方式,详情参见

  3. Excel VBA连接MySql 数据库获取数据

    编写Excel VBA工具,连接并操作Mysql 数据库. 系统环境: OS:Win7 64位 英文版 Office 2010 32位 英文版 1.VBA连接MySql前的准备 Tools---> ...

  4. Linux uname命令

    一.简介 uname 命令将正在使用的操作系统名写到标准输出中. 二.语法 -a 显示 -m. -n. -r. -s 和 -v 标志指定的所有信息.不能与 -x 或 -SName 标志连用.如果 -x ...

  5. Servlet对象生命周期(四)

    一.Servlet对象生命周期 一下图片说明上图第7点 destroy()方法是在停止tomcat服务器时执行 https://pan.baidu.com/s/1mgTabWW#list/path=% ...

  6. vimrc配置-中文编码和python中的中文注释

    set fileencoding=gb18030"设置vim输入的编码 set fileencodings=gb18030,...,"打开文档时vim自动匹配可能的编码方式 在py ...

  7. angular 分页

    http://jsfiddle.net/SAWsA/11/# <html xmlns:ng="http://angularjs.org" ng-app lang=" ...

  8. unit vs单元测试

    vs单元测试(unit) 一.什么是单元测试及它的作用? 在小量代码编写时,往往可以通过新建控制台项目(Console Application),新建网站项目(Web Form)等,在其中敲入测试代码 ...

  9. ubuntu - 14.04,安装Eclipse(开源开发工具)

    一,安装JDK:Eclipse必须有JDK才能运行,所以首先我们确定系统是否已经安装了JDK,我们在shell里面输入:“java -version”,如果已经安装了,就会打印出来当前JDK版本信息, ...

  10. Go语言学习教程:xorm表基本操作及高级操作

    在上节内容中,我们介绍了xorm框架表结构的映射规则和表结构的操作.本节课,继续来深入学习表结构基本操作和高级查询的相关功能. 表结构基本操作 对表结构的操作最常见的操作是查询和统计相关的方法,我们首 ...