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

推荐订阅源

T
Tenable Blog
H
Heimdal Security Blog
K
Kaspersky official blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Schneier on Security
G
GRAHAM CLULEY
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
CERT Recently Published Vulnerability Notes
Google DeepMind News
Google DeepMind News
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
C
Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
Security Archives - TechRepublic
Security Archives - TechRepublic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 司徒正美
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V
Visual Studio Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
Jina AI
Jina AI
P
Proofpoint News Feed
P
Proofpoint News Feed
有赞技术团队
有赞技术团队
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog
IT之家
IT之家
S
Security Affairs
博客园 - 叶小钗
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
N
News | PayPal Newsroom
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
NISL@THU
NISL@THU

博客园 - 三颗纽扣

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()