首頁 後端開發 C#.Net教程 從0自學C#13--子類別和父類別方法的鎖定物件問題

從0自學C#13--子類別和父類別方法的鎖定物件問題

Feb 04, 2017 am 11:14 AM

调用父类方法时,如何与子类方法,在锁对象不是同一个实例下,能线程安全,请见下面三种情况。

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

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

使用 C# 的活動目錄 使用 C# 的活動目錄 Sep 03, 2024 pm 03:33 PM

使用 C# 的 Active Directory 指南。在這裡,我們討論 Active Directory 在 C# 中的介紹和工作原理以及語法和範例。

C# 中的隨機數產生器 C# 中的隨機數產生器 Sep 03, 2024 pm 03:34 PM

C# 隨機數產生器指南。在這裡,我們討論隨機數產生器的工作原理、偽隨機數和安全數的概念。

C# 序列化 C# 序列化 Sep 03, 2024 pm 03:30 PM

C# 序列化指南。這裡我們分別討論C#序列化物件的介紹、步驟、工作原理和範例。

C# 資料網格視圖 C# 資料網格視圖 Sep 03, 2024 pm 03:32 PM

C# 資料網格視圖指南。在這裡,我們討論如何從 SQL 資料庫或 Excel 檔案載入和匯出資料網格視圖的範例。

C# 中的模式 C# 中的模式 Sep 03, 2024 pm 03:33 PM

C# 模式指南。在這裡,我們討論 C# 中模式的介紹和前 3 種類型,以及其範例和程式碼實作。

C# 中的質數 C# 中的質數 Sep 03, 2024 pm 03:35 PM

C# 質數指南。這裡我們討論c#中素數的介紹和範例以及程式碼實作。

C# 中的階乘 C# 中的階乘 Sep 03, 2024 pm 03:34 PM

C# 階乘指南。這裡我們討論 C# 中階乘的介紹以及不同的範例和程式碼實作。

c#多線程和異步的區別 c#多線程和異步的區別 Apr 03, 2025 pm 02:57 PM

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。

See all articles