Skip to content

Commit 0baa9b0

Browse files
committed
Singleton: add non-thread-safe version
1 parent 133e2e8 commit 0baa9b0

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

Singleton.Conceptual/Program.cs renamed to Singleton.Conceptual/NonThreadSafe/Program.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ class Singleton
2121
{
2222
private static Singleton _instance;
2323

24-
private static object _lock = new object();
25-
2624
private Singleton()
2725
{ }
2826

@@ -36,12 +34,13 @@ private Singleton()
3634
//
3735
// Эта реализация позволяет вам расширять класс Одиночки, сохраняя
3836
// повсюду только один экземпляр каждого подкласса.
39-
public static Singleton getInstance()
37+
public static Singleton GetInstance()
4038
{
41-
lock (_lock)
39+
if (_instance == null)
4240
{
43-
return _instance ?? (_instance = new Singleton());
41+
_instance = new Singleton();
4442
}
43+
return _instance;
4544
}
4645
}
4746

@@ -52,8 +51,8 @@ static void Main(string[] args)
5251
// EN: The client code.
5352
//
5453
// RU: Клиентский код.
55-
Singleton s1 = Singleton.getInstance();
56-
Singleton s2 = Singleton.getInstance();
54+
Singleton s1 = Singleton.GetInstance();
55+
Singleton s2 = Singleton.GetInstance();
5756

5857
if (s1 == s2)
5958
{

0 commit comments

Comments
 (0)