惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google Online Security Blog
Google Online Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
Cisco Talos Blog
Cisco Talos Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
Project Zero
Project Zero
E
Exploit-DB.com RSS Feed
S
Secure Thoughts
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
NISL@THU
NISL@THU
WordPress大学
WordPress大学
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
小众软件
小众软件
P
Privacy & Cybersecurity Law Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
About on SuperTechFans
Hacker News: Ask HN
Hacker News: Ask HN
AWS News Blog
AWS News Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hacker News: Front Page
F
Full Disclosure
Latest news
Latest news
Schneier on Security
Schneier on Security
The Hacker News
The Hacker News
T
Troy Hunt's Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Jina AI
Jina AI
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
G
GRAHAM CLULEY
Forbes - Security
Forbes - Security
V
V2EX - 技术
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Vulnerabilities – Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
MongoDB | Blog
MongoDB | Blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
P
Privacy International News Feed
C
Check Point Blog
N
News and Events Feed by Topic

博客园 - xvhfeng

blog搬迁 2012 年度总结 2011年年度总结 albian使用方法-1 albian开发笔记六-albian的架构 hashTable和多线程问题 albian开发笔记五--谈缓存同步 albian开发笔记四 albian开发笔记三 albian开发笔记二 albian开发笔记一 Albian开发库的设计与规划 《程序员入职锦囊妙计》终于完成了 FastMQ V0.2.0 stable版发布 发现你的优势测评结果 DFS架构 FastMQ V0.1.1使用方法 开源消息队列FastMQ V0.1.0 发布 linux环境C编程的血泪教训
实现线程内同一对象
xvhfeng · 2011-04-18 · via 博客园 - xvhfeng

       刚刚换公司,来到新公司后经历了一个星期的适应后正式开始干活。今天为同事解决了一个问题。问题的描述是这样的:首先程序是多线程的,要求就是对单线程内某一对象的访问,必须保证其对象在线程的生命周期内必须是同一个实例,线程是在thread pool中的,也就是说thread不会自己结束。

       因为已经2年没写.net程序了,所以上来有点生疏。依稀记得有一个特性可以搞定!同事让我过去看一下,一看上面写的是ThreadStatic,依稀记得是它。但是不太确定。然同事写着:

[ThreadStatic]
        static test obj = new test();

查了一下msdn,发现ThreadStatic这个属性是可以实现,但是不能声明+初始化对象放在一起,搞了半天,决定这样:

[ThreadStatic]
        static test obj ;
 
        public static test Obj
        {
            get
            {
                if (null == obj)
                {
                    obj = new test();
                    
                }
                return obj;
            }
        }

这样就ok了,但是查了一下msdn,实例只是对于valuetype的类型变量使用ThreadStatic,如果是引用对象的,那么推荐使用LocalDataStoreSlot来保存,小搞了一下,示例如下:

 static LocalDataStoreSlot localSlot;
 
    static ThreadStaticAttr()
    {
        localSlot = Thread.AllocateDataSlot();
    }
 
    public static void ThreadProc()
    {
        while (true)
        {
            if (null == Thread.GetData(localSlot))
                Thread.SetData(localSlot, new test());
 
            ((test)Thread.GetData(localSlot)).i++;
 
            Console.WriteLine("{0}:{1}", Thread.CurrentThread.Name, ((test)Thread.GetData(localSlot)).i);
            Thread.Sleep(500);
        }
    }

搞定后,又想了一种方法,其实可以使用Hashtable来自己实现,声明一个Synchronized的Hashtable,然后key使用Thread.CurrentThread.Name不就完了嘛?!实验后,果然奏效,代码如下:

private static Hashtable ht = Hashtable.Synchronized(new Hashtable());
    public static void ThreadProc()
    {
        while (true)
        {
            if (!ht.ContainsKey(Thread.CurrentThread.Name))
                ht.Add(Thread.CurrentThread.Name, new test());
   
         ((test)ht[Thread.CurrentThread.Name]).i++;
 
            Console.WriteLine("{0}:{1}", Thread.CurrentThread.Name, ((test)ht[Thread.CurrentThread.Name]).i);
            Thread.Sleep(500);
        }
    }

这3个示例使用的class非常的简单,如下:

 public class test
   {
       public int i = 0;
   }

而ThreadProc方法及时thread声明时的委托。