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--子类和父类要领的锁对象题目的内容,更多相关内容请关注ki4网(www.ki4.cn)!