从0自学C#13--子类和父类方法的锁对象问题
调用父类方法时,如何与子类方法,在锁对象不是同一个实例下,能线程安全,请见下面三种情况。
case1:
如下代码,在调用父类的方法时,和子类的方法,发生线程安全问题。原因的锁对象的实例不是同一个。
using System;using System.Collections.Generic; using System.Linq;using System.Text; using System.Threading.Tasks;using System.Threading; namespace ForTest//check the lock object for thread safety{ class Program { static void Main(string[] args) { son myson = new son(); Parallel.For(0, 5, (int i) => { myson.methodA(); myson.methodC(); }); Console.ReadKey(); } } public class grandfather { //protected static object syncRoot = new object(); } public class father:grandfather { private static object syncRoot = new object(); protected int cont = 0; public virtual bool methodA() { lock (syncRoot) { cont++; Thread.Sleep(1000); Console.WriteLine("cout++ is " + cont); return true; } } public virtual bool methodB() { lock (syncRoot) { return true; } } } public class son:father { private static object syncRoot = new object(); public bool methodC() { lock (syncRoot) { cont += 2; Thread.Sleep(2000); Console.WriteLine("cont += 2 is " + cont); return true; } } } }
登录后复制
输出:
cout++ is 1 cout++ is 4 cont += 2 is 5 cout++ is 5 cout++ is 8 cont += 2 is 9 cout++ is 11 cont += 2 is 11 cont += 2 is 13 cont += 2 is 15
登录后复制
case2:
case1的解决方法是,在父类初始化锁对象,让子类继承。这样就线程安全了。如下。
using System;using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace ForTest//check the lock object for thread safety{ class Program { static void Main(string[] args) { son myson = new son(); Parallel.For(0, 5, (int i) => { myson.methodA(); myson.methodC(); }); Console.ReadKey(); } } public class grandfather { protected static object syncRoot = new object(); } public class father:grandfather { protected int cont = 0; public virtual bool methodA() { lock (syncRoot) { cont++; Thread.Sleep(1000); Console.WriteLine("cout++ is " + cont); return true; } } public virtual bool methodB() { lock (syncRoot) { return true; } } } public class son:father { public bool methodC() { lock (syncRoot) { cont += 2; Thread.Sleep(2000); Console.WriteLine("cont += 2 is " + cont); return true; } } } }
登录后复制
输出:
cout++ is 1 cout++ is 2 cont += 2 is 4 cout++ is 5 cout++ is 6 cout++ is 7 cont += 2 is 9 cont += 2 is 11 cont += 2 is 13 cont += 2 is 15
登录后复制
登录后复制
case3:
当然有些特殊情况下,子类硬要重新实例化一个锁对象。如何避免上面第一种线程安全问题发生?需要:
子类加锁重写父类分方法(如果父类methodA是虚方法)
或者
new一下(如果父类methodA是实例方法)。
using System;using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace ForTest//check the lock object for thread safety{ class Program { static void Main(string[] args) { son myson = new son(); Parallel.For(0, 5, (int i) => { myson.methodA(); myson.methodC(); }); Console.ReadKey(); } } public class grandfather { protected static object syncRoot = new object(); } public class father:grandfather { protected int cont = 0; public virtual bool methodA() { lock (syncRoot) { cont++; Thread.Sleep(1000); Console.WriteLine("cout++ is " + cont); return true; } } public virtual bool methodB() { lock (syncRoot) { return true; } } } public class son:father { private static object sync = new object(); public override bool methodA()//重写 { lock (sync) { return base.methodA(); } } public bool methodC() { lock (sync) { cont += 2; Thread.Sleep(2000); Console.WriteLine("cont += 2 is " + cont); return true; } } } }
登录后复制
或
public new bool methodA()\\new一下 { lock (sync) { return base.methodA(); } }
登录后复制
输出:
cout++ is 1 cout++ is 2 cont += 2 is 4 cout++ is 5 cout++ is 6 cout++ is 7 cont += 2 is 9 cont += 2 is 11 cont += 2 is 13 cont += 2 is 15
登录后复制
登录后复制
以上就是 从0自学C#13--子类和父类方法的锁对象问题的内容,更多相关内容请关注PHP中文网(www.php.cn)!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前
By 尊渡假赌尊渡假赌尊渡假赌
刺客信条阴影:贝壳谜语解决方案
2 周前
By DDD
R.E.P.O.如果您听不到任何人,如何修复音频
1 个月前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
1 个月前
By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

使用 C# 的 Active Directory 指南。在这里,我们讨论 Active Directory 在 C# 中的介绍和工作原理以及语法和示例。

多线程和异步的区别在于,多线程同时执行多个线程,而异步在不阻塞当前线程的情况下执行操作。多线程用于计算密集型任务,而异步用于用户交互操作。多线程的优势是提高计算性能,异步的优势是不阻塞 UI 线程。选择多线程还是异步取决于任务性质:计算密集型任务使用多线程,与外部资源交互且需要保持 UI 响应的任务使用异步。
