ThreadStatic特性
地址:https://www.cnblogs.com/xuejietong/p/10997385.html
带有threadStaticAttribute标记的静态字段在线程之间不共享。每个执行线程都有一个单独的字段实例,并独立地设置和获取该字段的值。
如果在不同的线程上访问该字段,则它将包含不同的值。除了将threadStaticAttribute属性应用于字段之外,还必须将其定义为静态字段(在C中)或共享字段(在Visual Basic中)。不要为标记为threadStaticAttribute的字段指定初始值,因为此类初始化仅在类构造函数执行时发生一次(静态构造函数),因此只影响一个线程。
研究农业银行的支付接口demo时,发现代码中使用了这个特性:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text; namespace com.abc.trustpay.client
{
public class JSON
{
[ThreadStatic]
private static string iJsonString = ""; public static void setJsonString(string aJsonString)
{
JSON.iJsonString = aJsonString.Trim();
} public static string WriteDictionary(IDictionary dic)
{
StringBuilder stringBuilder = new StringBuilder("");
bool flag = false;
stringBuilder.Append("{");
foreach (DictionaryEntry dictionaryEntry in dic)
{
if (flag)
{
stringBuilder.Append(",");
}
stringBuilder.Append("\"");
stringBuilder.Append(dictionaryEntry.Key);
stringBuilder.Append("\":");
stringBuilder.Append("\"");
stringBuilder.Append(dictionaryEntry.Value);
stringBuilder.Append("\"");
flag = true;
}
return stringBuilder.ToString();
} public static string WriteDictionarys(IDictionary dic)
{
StringBuilder stringBuilder = new StringBuilder("");
bool flag = false;
stringBuilder.Append("[");
foreach (int num in dic.Keys)
{
if (flag)
{
stringBuilder.Append("},");
}
string value = JSON.WriteDictionary((IDictionary)dic[num]);
stringBuilder.Append(value);
flag = true;
}
stringBuilder.Append("}]");
return stringBuilder.ToString();
} public static string GetKeyValue(string aTag)
{
string text = JSON.iJsonString;
int num = text.IndexOf(aTag);
string result;
if (num == -1)
{
result = "";
}
else
{
while (!text.Substring(num - 1, 1).Equals("\"") || !text.Substring(num + aTag.Length, 1).Equals("\""))
{
num = text.IndexOf(aTag, num + 1);
if (num == -1)
{
result = "";
return result;
}
}
int length = aTag.Length;
num = num + length + 2;
string text2 = "";
char c = text[num];
if (c == '{')
{
text2 = JSON.GetObjectValue(num, aTag, text);
}
if (c == '"')
{
text2 = JSON.GetStringValue(num, aTag, text);
}
result = text2;
}
return result;
} private static string GetObjectValue(int index, string aTag, string json)
{
int num = 0;
StringBuilder stringBuilder = new StringBuilder();
char c;
while (true)
{
c = json[index];
if (c == '{')
{
num++;
}
if (c == '}')
{
num--;
}
if (num == 0)
{
break;
}
stringBuilder.Append(c);
index++;
}
stringBuilder.Append(c);
return stringBuilder.ToString();
} private static string GetStringValue(int index, string aTag, string json)
{
StringBuilder stringBuilder = new StringBuilder();
index++;
while (true)
{
char c = json[index++];
if (c == '"')
{
break;
}
stringBuilder.Append(c);
}
return stringBuilder.ToString();
} public static Dictionary<int, Hashtable> GetArrayValue(string aTag)
{
string text = JSON.iJsonString;
Dictionary<int, Hashtable> dictionary = new Dictionary<int, Hashtable>();
Hashtable hashtable = new Hashtable();
StringBuilder stringBuilder = new StringBuilder();
int num = text.IndexOf(aTag);
Dictionary<int, Hashtable> result;
if (num == -1)
{
result = dictionary;
}
else
{
int length = aTag.Length;
num = num + length + 2;
int num2 = 0;
while (true)
{
char c = text[num++];
if (c != '[')
{
if (c != ']')
{
stringBuilder.Append(c);
}
if (c == '}')
{
c = text[num++];
hashtable.Add(num2, stringBuilder);
num2++;
stringBuilder = new StringBuilder();
}
if (c == ']')
{
break;
}
}
}
if (hashtable.Count == 0)
{
result = dictionary;
}
else
{
dictionary = JSON.ParseArray(hashtable);
result = dictionary;
}
}
return result;
} public static Dictionary<int, Hashtable> ParseArray(Hashtable hts)
{
Dictionary<int, Hashtable> dictionary = new Dictionary<int, Hashtable>();
Hashtable value = new Hashtable();
foreach (int num in hts.Keys)
{
string json = hts[num].ToString();
value = JSON.ParseString(json);
dictionary.Add(num, value);
value = new Hashtable();
}
return dictionary;
} public static Hashtable ParseString(string json)
{
StringBuilder stringBuilder = new StringBuilder();
Hashtable hashtable = new Hashtable();
int num = json.IndexOf("{");
int length = json.Length;
json = json.Substring(num + 1, length - 2);
json = json.Replace("\"", "");
string[] array = json.Split(new char[]
{
','
});
string[] array2 = new string[2];
for (int i = 0; i < array.Length; i++)
{
array2 = array[i].Split(new char[]
{
':'
});
hashtable.Add(array2[0], array2[1]);
}
return hashtable;
}
}
}
ThreadStatic特性的更多相关文章
- AsyncLocal 与 ThreadLocal ThreadStatic特性简介
AsyncLocal 与 ThreadLocal [.NET深呼吸]基于异步上下文的本地变量(AsyncLocal) https://www.cnblogs.com/tcjiaan/p/5007737 ...
- ThreadStatic应用(Identity补完)
关于Identity Identity自增序列/唯一断标识 起初做这个东西,是在一个内部组件中,用于在高并发的环境下得到一个较短的“相对”不重复标识字符串;(这里说的相对是指一定的数量下不重复) 灵感 ...
- 基础才是重中之重~关于ThreadStatic和Quartz的一点渊源
回到目录 ThreadStatic ThreadStatic是C#里的一个特性,它可以让你的字段在一个线程里有效,但你不能控制这个字段在何时被回收,即如果声明一个int32的字段为ThreadStat ...
- 被 C# 的 ThreadStatic 标记的静态变量,都存放在哪里了?
一:背景 1. 讲故事 前几天公号里有一位朋友留言说,你windbg玩的溜,能帮我分析下被 ThreadStatic 修饰的变量到底存放在哪里吗?能不能帮我挖出来,其实这个问题问的挺深的,玩高级语言的 ...
- 【C# 线程】线程局部存储(TLS)理论部分 ThreadStatic|LocalDataStoreSlot|ThreadLocal<T>
线程本地存储(TLS:Thread Local Storage) 线程本地存储(Thread Local Storage),字面意思就是专属某个线程的存储空间.变量大体上分为全局变量和局部变量,一个进 ...
- .NET基础拾遗(5)多线程开发基础
Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理基础 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开 ...
- [.net 面向对象程序设计进阶] (17) 多线程(Multithreading)(二) 利用多线程提高程序性能(中)
[.net 面向对象程序设计进阶] (17) 多线程(Multithreading)(二) 利用多线程提高程序性能(中) 本节要点: 上节介绍了多线程的基本使用方法和基本应用示例,本节深入介绍.NET ...
- .Net面試題
初级.NET开发人员 - 任何使用.NET的人都应知道的 1. 描述线程与进程的区别? 进程是系统所有资源分配时候的一个基本单位,拥有一个完整的虚拟空间地址,并不依赖线程而独立存在.进程可以定义程序的 ...
- 多线程中Local Store Slot(本地存储槽)
在Java中有一种ThreadLocal机制,为每一个使用该变量的线程都提供一个变量值的副本,是每一个线程都可以独立地改变自己的副本,而不会和其它线程的副本冲突.从线程的角度看,就好像每一个线程都完全 ...
随机推荐
- python脚本容器化
https://blog.csdn.net/zstack_org/article/details/53099919 如何选择 base image: https://blog.csdn.net/nkl ...
- [LeetCode] 203. Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- [LeetCode] 529. Minesweeper 扫雷
Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...
- docker tag根据镜像id做标签,用于应用的回滚
示例 通过ID tag镜像 下面是tag一个id为0e5574283393的本地镜像到“fedora”存储库,tag名称version1.0: docker tag 0e5574283393 fedo ...
- 【计算机视觉】目标检测中的指标衡量Recall与Precision
[计算机视觉]目标检测中的指标衡量Recall与Precision 标签(空格分隔): [图像处理] 说明:目标检测性能指标Recall与Precision的理解. Recall与Precision ...
- phpexcel无法导出的解决方法
phpexcel无法导出的解决方法 <pre> set_time_limit(0); ini_set("memory_limit","512M"); ...
- 小程序常用操作,if,for,跳转,弹出提示
if <block wx:if="{{result.child_items}}"> ... </block> <block wx:else> . ...
- LeetCode 537. 复数乘法(Complex Number Multiplication)
537. 复数乘法 537. Complex Number Multiplication 题目描述 Given two strings representing two complex numbers ...
- [转帖]都在说DCEP,央行数字货币究竟跟你有什么关系?
都在说DCEP,央行数字货币究竟跟你有什么关系? https://kuaibao.qq.com/s/20191104A0G1D300?refer=spider 黄奇帆指出,DCEP 使得交易环节对 ...
- 在Jenkins的pipeline项目中运行jmeter测试-教程
Jenkins 2.0的发布引入了一种新的项目类型 - Pipeline,以前只能通过插件获得.从Jenkins 2.0开始,Pipeline项目开箱即用. 与通常的“自由式”项目相比,管道构建具有几 ...