技術群裡有人發了一段程式碼:
附言:兄弟們,這個單例怎麼樣?
我回復:什麼鬼,看不懂啊?!
也有其他小夥伴表示看不懂,看來大家的C#基礎和我一樣並不全面。
我看不懂,主要是因為我沒用過TaskCompletionSource和Interlocked的CompareExchange方法,然後經過我1、2個小時的研究,終於勉強看懂了。
由於上面這段程式碼只貼了一張圖,我沒有拿到原始碼,所以我寫了個差不多的Demo用於測試,程式碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace SingletonTest { public class Singleton { private static Task<string> _stringTask; /// <summary> /// 重置,方便重複測試 /// </summary> public void Reset() { _stringTask = null; } public Task<string> InitAsync() { if (_stringTask != null) { return _stringTask; } var inition = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously); var initonTask = Interlocked.CompareExchange(ref _stringTask, inition.Task, null); if (initonTask != null) { return initonTask; } _stringTask = CreateContent(inition); return inition.Task; } private async Task<string> CreateContent(TaskCompletionSource<string> inition) { string content = await TextUtil.GetTextAsync(); inition.SetResult(content); return content; } } }
然後按照我自己的習慣,又寫了一版:
using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace SingletonTest { class Singleton2 { private static string _value; private SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1); /// <summary> /// 重置,方便重複測試 /// </summary> public void Reset() { _value = null; } public async Task<string> InitAsync() { if (_value != null) { return _value; } await _semaphoreSlim.WaitAsync(); if (_value == null) { _value = await TextUtil.GetTextAsync(); } _semaphoreSlim.Release(); return _value; } } }
很容易懂,不是嗎?
這段程式碼我好像是理解了,可是我不理解的是,為什麼程式碼會寫的這麼複雜呢?
最主要的是我不理解下面幾行:
var inition = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously); var initonTask = Interlocked.CompareExchange(ref _stringTask, inition.Task, null); if (initonTask != null) { return initonTask; }
我要給它翻譯成我能理解的程式碼,我意思到new的TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously)也是個單例,所以我先寫了個TaskCompletionSourceFactory類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace SingletonTest { public class TaskCompletionSourceFactory : IDisposable { private TaskCompletionSource<string> _value; private TaskCompletionSourceData _data; private SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1); public TaskCompletionSourceData Instance { get { _semaphoreSlim.Wait(); if (_value == null) { _data = new TaskCompletionSourceData(); _value = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously); _data.Value = _value; _data.First = true; } else { _data = new TaskCompletionSourceData(); _data.Value = _value; _data.First = false; } _semaphoreSlim.Release(); return _data; } } public void Dispose() { _semaphoreSlim.Dispose(); } } public class TaskCompletionSourceData { public bool First { get; set; } public TaskCompletionSource<string> Value { get; set; } } }
然後把Demo中Singleton這個類改寫了一下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace SingletonTest { public class Singleton3 { private static Task<string> _stringTask; /// <summary> /// 重置,方便重複測試 /// </summary> public void Reset() { _stringTask = null; } public Task<string> InitAsync(TaskCompletionSourceFactory factory) { if (_stringTask != null) { return _stringTask; } var inition = factory.Instance; if (!inition.First) { return inition.Value.Task; } _stringTask = CreateContent(inition.Value); return inition.Value.Task; } private async Task<string> CreateContent(TaskCompletionSource<string> inition) { string content = await TextUtil.GetTextAsync(); inition.SetResult(content); return content; } } }
當我差不多理解了之後,我發現原始程式碼有一點點小問題,就是TaskCompletionSource<string>是有機率被重複new的。
大家覺得哪種寫法好呢?
附:
TextUtil.cs程式碼,是一個模擬獲取文字的方法:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace SingletonTest { public class TextUtil { public static Task<string> GetTextAsync() { return Task.Run<string>(() => { Thread.Sleep(10); Random rnd = new Random(); return rnd.Next(0, 1000).ToString().PadRight(10); }); } } }
測試程式碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace SingletonTest { class Program { private static int _count = 200; private static Singleton _singleton = new Singleton(); private static Singleton2 _singleton2 = new Singleton2(); private static Singleton3 _singleton3 = new Singleton3(); static void Main(string[] args) { ThreadPool.SetMinThreads(20, 20); Task.Run(() => { }); //Task預熱 Console.WriteLine("輸入1測試Singleton,輸入2測試Singleton2,如果值都相同,說明單例測試通過,否則不通過"); while (true) { var key = Console.ReadKey().Key; if (key == ConsoleKey.D1) { Console.WriteLine("測試Singleton"); Test(); } if (key == ConsoleKey.D2) { Console.WriteLine("測試Singleton2"); Test2(); } if (key == ConsoleKey.D3) { Console.WriteLine("測試Singleton3"); Test3(); } } } public static void Test() { List<Task> taskList = new List<Task>(); for (int i = 0; i < _count; i++) { Task task = Task.Run(async () => { string content = await _singleton.InitAsync(); Console.Write(content); }); taskList.Add(task); } Task.WaitAll(taskList.ToArray()); _singleton.Reset(); Console.WriteLine(""); } public static void Test2() { List<Task> taskList = new List<Task>(); for (int i = 0; i < _count; i++) { Task task = Task.Run(async () => { string content = await _singleton2.InitAsync(); Console.Write(content); }); taskList.Add(task); } Task.WaitAll(taskList.ToArray()); _singleton2.Reset(); Console.WriteLine(""); } public static void Test3() { TaskCompletionSourceFactory factory = new TaskCompletionSourceFactory(); List<Task> taskList = new List<Task>(); for (int i = 0; i < _count; i++) { Task task = Task.Run(async () => { string content = await _singleton3.InitAsync(factory); Console.Write(content); }); taskList.Add(task); } Task.WaitAll(taskList.ToArray()); _singleton3.Reset(); factory.Dispose(); Console.WriteLine(""); } } }