调用父类方法时,如何与子类方法,在锁对象不是同一个实例下,能线程安全,请见下面三种情况。
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)!

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#在.NET中的编程过程包括以下步骤:1)编写C#代码,2)编译为中间语言(IL),3)由.NET运行时(CLR)执行。C#在.NET中的优势在于其现代化语法、强大的类型系统和与.NET框架的紧密集成,适用于从桌面应用到Web服务的各种开发场景。

C#是一种现代、面向对象的编程语言,由微软开发并作为.NET框架的一部分。1.C#支持面向对象编程(OOP),包括封装、继承和多态。2.C#中的异步编程通过async和await关键字实现,提高应用的响应性。3.使用LINQ可以简洁地处理数据集合。4.常见错误包括空引用异常和索引超出范围异常,调试技巧包括使用调试器和异常处理。5.性能优化包括使用StringBuilder和避免不必要的装箱和拆箱。

C#.NET应用的测试策略包括单元测试、集成测试和端到端测试。1.单元测试确保代码的最小单元独立工作,使用MSTest、NUnit或xUnit框架。2.集成测试验证多个单元组合的功能,常用模拟数据和外部服务。3.端到端测试模拟用户完整操作流程,通常使用Selenium进行自动化测试。

C#高级开发者面试需要掌握异步编程、LINQ、.NET框架内部工作原理等核心知识。1.异步编程通过async和await简化操作,提升应用响应性。2.LINQ以SQL风格操作数据,需注意性能。3..NET框架的CLR管理内存,垃圾回收需谨慎使用。

C#.NET面试问题和答案包括基础知识、核心概念和高级用法。1)基础知识:C#是微软开发的面向对象语言,主要用于.NET框架。2)核心概念:委托和事件允许动态绑定方法,LINQ提供强大查询功能。3)高级用法:异步编程提高响应性,表达式树用于动态代码构建。

C#.NET是构建微服务的热门选择,因为其生态系统强大且支持丰富。1)使用ASP.NETCore创建RESTfulAPI,处理订单创建和查询。2)利用gRPC实现微服务间的高效通信,定义和实现订单服务。3)通过Docker容器化微服务,简化部署和管理。

C#和.NET的安全最佳实践包括输入验证、输出编码、异常处理、以及身份验证和授权。1)使用正则表达式或内置方法验证输入,防止恶意数据进入系统。2)输出编码防止XSS攻击,使用HttpUtility.HtmlEncode方法。3)异常处理避免信息泄露,记录错误但不返回详细信息给用户。4)使用ASP.NETIdentity和Claims-based授权保护应用免受未授权访问。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

WebStorm Mac版
好用的JavaScript开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。