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

推荐订阅源

W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
F
Full Disclosure
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threatpost
I
Intezer
V2EX - 技术
V2EX - 技术
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
小众软件
小众软件
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
N
News | PayPal Newsroom
MyScale Blog
MyScale Blog
AI
AI
Vercel News
Vercel News
Spread Privacy
Spread Privacy
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
U
Unit 42
L
LangChain Blog
Recent Announcements
Recent Announcements

博客园 - 三颗纽扣

windows下安装redmine-2.1 使用InfoBright实现20-100亿条原始话单记录的检索 Hibernate乐观锁真的会抛出异常吗? 并非文学化编程,在VS环境下的折中方案 快速复制NuGet引用 语义版本规范 基础框架的基础组件 在 IIS 6 上架设 NuGet Server DotNet开发利器之MyEclipseShortcuts 通过 POI 获取图片在 Excel 表格中的位置 我们是原始生物 没有别的,只有天使 灯是用来照亮的,而不是引路的 戒了,过去完成时 我送给你们的,没有别的——只有天使。 Builder 链——另类一点的Builder模式 多线程JUnit单元测试:GroboUtils and ConTest 控制内存的使用之二:对象缓存 pool and cache 控制内存的使用
try-cache-finally 在线程被杀掉时还有作用吗?
三颗纽扣 · 2012-10-28 · via 博客园 - 三颗纽扣

问题起因是因为在线程中申请了读写锁,如果一个线程被杀掉没有机会释放锁,那么杀掉线程就是一个很危险的操作,可能导致其他线程申请锁时被死锁。因此实验了一下。结论是即使线程被杀,try-cache-finally 依然有效,因为实际上抛出了线程终止异常ThreadAbortException,不多废话,看结果。

        [TestFixture]
        private class Test
        {
            ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();

            class Locker : IDisposable
            {
                ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
                public Locker(ReaderWriterLockSlim lockSlim)
                {
                    _lock = lockSlim;
                    _lock.EnterReadLock();
                }
                public void Dispose()
                {
                    if (_lock == null) return;
                    var locker = _lock;
                    _lock = null;
                    locker.ExitReadLock();
                    Console.WriteLine("thread3 released");
                }
            }

            [Test]
            public void TestMethod()
            {
                var t1 = new Thread(Thread1){Name = "thread1"};
                var t2 = new Thread(Thread2) { Name = "thread2" };
                var t3 = new Thread(Thread3) { Name = "thread3" };
                t1.Start();
                t2.Start();
                t3.Start();
                Thread.Sleep(300);

                t2.Abort();
                t3.Abort();
                Thread.Sleep(1000);
                t1.Abort();

                t1.Join();
                t2.Join();
                t3.Join();
            }

            public void Thread1()
            {
                Thread.Sleep(1000);
                _lock.EnterWriteLock();
                Console.WriteLine("thread1 locked");
                Thread.Sleep(5000);
                _lock.ExitWriteLock();
                Console.WriteLine("thread1 released");
            }

            public void Thread2()
            {
                _lock.EnterReadLock();
                Console.WriteLine("thread2 locked");
                try
                {
                    while (true)
                    {
                        // NOP
                    }
                }
                finally
                {                    
                    _lock.ExitReadLock();
                    Console.WriteLine("thread2 released");
                }
            }

            public void Thread3()
            {
                using (new Locker(_lock))
                {
                    Console.WriteLine("thread3 locked");
                    while (true)
                    {
                        // NOP
                    }
                }
            }

        }

 输出结果:

thread2 locked
thread3 locked
---- UNHANDLED EXCEPTION ----
Thread Name: thread2
System.Threading.ThreadAbortException: 正在中止线程。
   在 Wellcomm.Dts.Xmq.XmqQueue.Test.Thread2() 位置 D:\shwen\DotNetProjects\Wellcomm.Dts\Wellcomm.Dts.Xmq\XmqQueue.cs:行号 636
   在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   在 System.Threading.ExecutionContext.runTryCode(Object userData)
   在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   在 System.Threading.ThreadHelper.ThreadStart()
thread2 released
---- UNHANDLED EXCEPTION ----
Thread Name: thread3
System.Threading.ThreadAbortException: 正在中止线程。
   在 Wellcomm.Dts.Xmq.XmqQueue.Test.Thread3() 位置 D:\shwen\DotNetProjects\Wellcomm.Dts\Wellcomm.Dts.Xmq\XmqQueue.cs:行号 653
   在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   在 System.Threading.ExecutionContext.runTryCode(Object userData)
   在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   在 System.Threading.ThreadHelper.ThreadStart()
thread3 released
thread1 locked
---- UNHANDLED EXCEPTION ----
Thread Name: thread1
System.Threading.ThreadAbortException: 正在中止线程。
   在 System.Threading.Thread.SleepInternal(Int32 millisecondsTimeout)
   在 System.Threading.Thread.Sleep(Int32 millisecondsTimeout)
   在 Wellcomm.Dts.Xmq.XmqQueue.Test.Thread1() 位置 D:\shwen\DotNetProjects\Wellcomm.Dts\Wellcomm.Dts.Xmq\XmqQueue.cs:行号 625
   在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   在 System.Threading.ExecutionContext.runTryCode(Object userData)
   在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   在 System.Threading.ThreadHelper.ThreadStart()