1. 使用場景
來著stackorverflow
2. 簡單模式
public class BaseSingle
{
public void Method()
{
Console.WriteLine("{1}.Method:{0}", this.GetHashCode(),this.GetType().ToString());
}
}
public class UnsafeSingle : BaseSingle
{
private static UnsafeSingle _single;
public static UnsafeSingle UnsafeSingleInstatce()
{
if (null == _single)
_single = new UnsafeSingle();
return _single;
}
private UnsafeSingle()
{
Console.WriteLine(DateTime.Now.ToString() + ": 構造函數!");
}
}
缺點:對象未創建時,多線程同時訪問返回的對象並非一個;
測試代碼如下:
internal delegate void InstanceFun();
public void GetInstatnce()
{
UnsafeSingle.UnsafeSingleInstatce().Method();
}
public void MutilThreadTest(InstanceFun instanceFun)
{
var started = DateTime.Now;
Thread[] threads = new Thread[100];
for (var index = 0; index < 100; index++)
{
Thread thread = new Thread(new ThreadStart(instanceFun));
threads[index] = thread;
}
foreach (var thread in threads)
{
thread.Start();
}
Console.WriteLine("運行時長:{0};僅臨時測試", (DateTime.Now - started).Milliseconds);
}
測試結果:
DesignPattern.UnsafeSingle.Method:45338574
DesignPattern.UnsafeSingle.Method:30944461
DesignPattern.UnsafeSingle.Method:62452338
DesignPattern.UnsafeSingle.Method:43921088
DesignPattern.UnsafeSingle.Method:18573725
DesignPattern.UnsafeSingle.Method:8935080
DesignPattern.UnsafeSingle.Method:48823647
DesignPattern.UnsafeSingle.Method:28613802
DesignPattern.UnsafeSingle.Method:40642054
DesignPattern.UnsafeSingle.Method:52962918
DesignPattern.UnsafeSingle.Method:29095347
DesignPattern.UnsafeSingle.Method:62694277
DesignPattern.UnsafeSingle.Method:54643837
DesignPattern.UnsafeSingle.Method:36302867
DesignPattern.UnsafeSingle.Method:49913678
DesignPattern.UnsafeSingle.Method:52482287
DesignPattern.UnsafeSingle.Method:41829035
DesignPattern.UnsafeSingle.Method:57978397
DesignPattern.UnsafeSingle.Method:13456355
DesignPattern.UnsafeSingle.Method:23736788
DesignPattern.UnsafeSingle.Method:42195492
DesignPattern.UnsafeSingle.Method:28113429
DesignPattern.UnsafeSingle.Method:2875151
DesignPattern.UnsafeSingle.Method:56858914
DesignPattern.UnsafeSingle.Method:2735313
DesignPattern.UnsafeSingle.Method:50717245
DesignPattern.UnsafeSingle.Method:30697644
DesignPattern.UnsafeSingle.Method:47515266
DesignPattern.UnsafeSingle.Method:30183720
DesignPattern.UnsafeSingle.Method:65195544
DesignPattern.UnsafeSingle.Method:22695823
DesignPattern.UnsafeSingle.Method:8561069
DesignPattern.UnsafeSingle.Method:33135541
DesignPattern.UnsafeSingle.Method:17719913
DesignPattern.UnsafeSingle.Method:32493892
DesignPattern.UnsafeSingle.Method:57339132
DesignPattern.UnsafeSingle.Method:38516275
DesignPattern.UnsafeSingle.Method:33858004
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
DesignPattern.UnsafeSingle.Method:58042889
運行時長:115
DesignPattern.UnsafeSingle.Method:58042889
後續更新