Singlton实施 [英] Singlton implementation

查看:59
本文介绍了Singlton实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何在c#中使用单个元素。我已经完成了

的实现,但我跑到了双重检查锁定警报

实现,而不知道如何实现它。或者让我把它这个

的方式,相当于以下java代码来解决

双重检查锁定警报???


private volatile static Singlton uniqueInstance


public static Singlton getInstance()

{

if(uniqueInstance = = null)

{

synchornized(Singlton.class){< - 我们如何在c#

if(uniqueInstance = = null){

uniqueInstance = new Singlton();

}

}

}

}

Does any one know how to implment a singlton in c#. I have done the
implementation but I ran in to "Double-checked locking alert"
implemenation and not sure how to implement it. Or let me put it this
way, what is the equivalent of the following java code to resolve the
double-checked locking alert???

private volatile static Singlton uniqueInstance

public static Singlton getInstance()
{
if (uniqueInstance == null)
{
synchornized(Singlton.class) { <-- how can we do this in c#
if (uniqueInstance == null){
uniqueInstance = new Singlton();
}
}
}
}

推荐答案

DBC用户,


以下文章介绍了什么详细要求并提供

其他建议。

http://www.yoda.arachsys.com/csharp/singleton.html


Brian


DBC用户写道:
DBC User,

The following article describes what''s required in detail and offers
other suggestions as well.

http://www.yoda.arachsys.com/csharp/singleton.html

Brian

DBC User wrote:

是否有人知道如何在c#中使用singlton。我已经完成了

的实现,但我跑到了双重检查锁定警报

实现,而不知道如何实现它。或者让我把它这个

的方式,相当于以下java代码来解决

双重检查锁定警报???


private volatile static Singlton uniqueInstance


public static Singlton getInstance()

{

if(uniqueInstance = = null)

{

synchornized(Singlton.class){< - 我们如何在c#

if(uniqueInstance = = null){

uniqueInstance = new Singlton();

}

}

}

}
Does any one know how to implment a singlton in c#. I have done the
implementation but I ran in to "Double-checked locking alert"
implemenation and not sure how to implement it. Or let me put it this
way, what is the equivalent of the following java code to resolve the
double-checked locking alert???

private volatile static Singlton uniqueInstance

public static Singlton getInstance()
{
if (uniqueInstance == null)
{
synchornized(Singlton.class) { <-- how can we do this in c#
if (uniqueInstance == null){
uniqueInstance = new Singlton();
}
}
}
}


DBC用户写道:
DBC User wrote:

有没有人知道如何在c#中表达一个单一的。我已经完成了

的实现,但我跑到了双重检查锁定警报

实现,而不知道如何实现它。或者让我把它这个

的方式,相当于以下java代码来解决

双重检查锁定警报???


private volatile static Singlton uniqueInstance


public static Singlton getInstance()

{

if(uniqueInstance = = null)

{

synchornized(Singlton.class){< - 我们如何在c#

if(uniqueInstance = = null){

uniqueInstance = new Singlton();

}

}

}

}
Does any one know how to implment a singlton in c#. I have done the
implementation but I ran in to "Double-checked locking alert"
implemenation and not sure how to implement it. Or let me put it this
way, what is the equivalent of the following java code to resolve the
double-checked locking alert???

private volatile static Singlton uniqueInstance

public static Singlton getInstance()
{
if (uniqueInstance == null)
{
synchornized(Singlton.class) { <-- how can we do this in c#
if (uniqueInstance == null){
uniqueInstance = new Singlton();
}
}
}
}



private static Singlton instance = null;

私有静态对象mylock = new object();

public static Singlton Instance

{

get {

lock(mylock)

{

if(instance == null)

{

instance = new Singlton();

}

}

返回实例;

}

}


是我称之为规范形式。


注意:双重锁定在Java中无法正常运行

时间我读到判决结果是它在.NET中不起作用

(在x86平台上偶然工作)。


Arne

private static Singlton instance = null;
private static object mylock = new object();
public static Singlton Instance
{
get {
lock(mylock)
{
if(instance == null)
{
instance = new Singlton();
}
}
return instance;
}
}

is what I would call the canonical form.

NB: Double locking does not work properly in Java and last
time I read the verdict was that it did not work in .NET
either (just works accidentally on x86 platform).

Arne


Brian Gideon写道:
Brian Gideon wrote:

以下文章描述了详细要求和提供的内容

其他建议。

http://www.yoda.arachsys .com / csharp / singleton.html



这是一篇非常好的文章,在C#和.NET运行时都有一些有趣的

洞察力。


但如果所有程序员都认为他们必须编写其中一个花哨的解决方案,我不喜欢它。


除非有特殊要求,否则简单的

解决方案通常都是好的嗯,并且赚了很多

人们更容易阅读代码。


Arne

It is a very good article with some interesting
insight in both C# and .NET runtime.

But I do not like it if all programmer think
that they have to write one of the fancy solutions.

Unless one have special requirements the simple
solution is usually good enough and make it a lot
easier for people to read the code.

Arne


这篇关于Singlton实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆