程序源码如下
using System;
using System.Data;
using System.Threading;namespace Singleton_Pattern
{
public sealed class CountSingleton
{
static readonly CountSingleton iuCount = new CountSingleton();
private int totNum = 0; private CountSingleton()
{} public void Add()
{
this.totNum++;
} public int GetValue
{
get
{
return this.totNum;
}
} public static CountSingleton Instance
{
get
{
return iuCount;
}
}
} public class EntryProgram
{
public static void Main()
{
for(int i = 0; i < 3; i++)
{
CountSingleton singleton = CountSingleton.Instance;
singleton.Add();
Console.WriteLine(singleton.GetValue);
}
Console.ReadLine();
}
}
}
我的问题
1.每一次执行for语句时CountSingleton类会依次执行以后语句
static readonly CountSingleton iuCount = new CountSingleton();
private int totNum = 0;private CountSingleton()
{}static readonly CountSingleton iuCount = new CountSingleton();静态类的初始化执行了两次,是因为new CountSingleton();的原因才执行的两次吗,具体是什么原因2.每1次以后再执行for语句时不再执行问题1中的变量初始化语句,直接返回CountSingleton.Instance属性提供的实例,为什么只有第一次才执行静态成员的初始化工作?
using System;
using System.Data;
using System.Threading;namespace Singleton_Pattern
{
public sealed class CountSingleton
{
static readonly CountSingleton iuCount = new CountSingleton();
private int totNum = 0; private CountSingleton()
{} public void Add()
{
this.totNum++;
} public int GetValue
{
get
{
return this.totNum;
}
} public static CountSingleton Instance
{
get
{
return iuCount;
}
}
} public class EntryProgram
{
public static void Main()
{
for(int i = 0; i < 3; i++)
{
CountSingleton singleton = CountSingleton.Instance;
singleton.Add();
Console.WriteLine(singleton.GetValue);
}
Console.ReadLine();
}
}
}
我的问题
1.每一次执行for语句时CountSingleton类会依次执行以后语句
static readonly CountSingleton iuCount = new CountSingleton();
private int totNum = 0;private CountSingleton()
{}static readonly CountSingleton iuCount = new CountSingleton();静态类的初始化执行了两次,是因为new CountSingleton();的原因才执行的两次吗,具体是什么原因2.每1次以后再执行for语句时不再执行问题1中的变量初始化语句,直接返回CountSingleton.Instance属性提供的实例,为什么只有第一次才执行静态成员的初始化工作?
没有人会吗
----------------
明显挑衅,没必要的,很多人还是很热心的。关于问题二,static是只会初始化一次的,包括static的构造函数
然后,单件模式,我见的是这样的写法:
static readonly CountSingleton iuCount = null;
public static CountSingleton Instance
{
get
{
if (iuCount == null)
iuCount = new CountSingleton();
return iuCount;
}
}
static readonly CountSingleton Instance = new CountSingleton();
楼主的代码没有问题
问题1:这几行代码指执行了一次。只是单步调试时发现黄色的箭头会有两次指向
static readonly CountSingleton iuCount = new CountSingleton();
但是这并不表示他执行了两次,后一次指向只是表示该语句执行完毕
问题2:static的东西,包括变量阿构造函数阿,都只执行一次。
没有人会吗
---------------------------------
看到这句觉得不太爽!本来想不答了!1.静态构造函数一定只执行一次。
2.静态成员的初始化实际上编译后包括在静态构造函数中,这也就变成了问题1。参看《.NET框架程序设计》
2.程序执行一次后CountSingleton实例已经放到内存,由于是static的所以第二次运行时没有执行初始化。
你在三楼的方法显然不是线程安全的,如果两上线程同时进行
if (iuCount == null)
iuCount = new CountSingleton();
return iuCount;
怎以办,不就违背了单件模式的定义了吗?
这两个问题我已经弄明白了,至于第一个问题楼上几位朋友还是没有搞清楚我说的两次是什么意思,我在这里解释一下
第一次执行static readonly CountSingleton iuCount = new CountSingleton();是因为这个类被第一次调用,当类中所有字段初始化以手才会执行构造方法,虽然iuCount是static修饰的,但他是new出来的对象,执行二次的原因是只执行了new CountSingleton();部份,仅在内存的stack区中分配一个块区域
static readonly CountSingleton iuCount = new CountSingleton();
不管是静态还是非静态,最终都被放到了构造函数中(静态的放到静态构造函数最前面,非静态的放到普通构造函数最前面)
即编译后的代码等同于如下:
static readonly CountSingleton iuCount;
static CountSingliton()
{
iuCount = new CountSingleton();
//假如原本就有静态构造函数,那么其中的代码就在其后面
}
这样你就该明白为什么我解释的似乎跟你要的解释是两回事了。你的写法就创建来说,是线程安全的。
上面的可以改一下:
static readonly CountSingleton iuCount = null;
public static CountSingleton Instance
{
get
{
if (iuCount == null)
{
lock (typeof(CountSingleton))
{
if (iuCount == null)
iuCount = new CountSingleton();
}
}
return iuCount;
}
}
这样就是线程安全的了。