线程安全单例 [英] Thread safe singleton

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

问题描述

我有一个VB.net dll项目,其中一个类是一个单例。我已经在winform应用程序中使用这个没有任何问题的
。我想在web表单项目中使用这个

相同的dll,但我的单例会导致问题,因为

某些会话可能需要单例中的不同值。我想更改

我的单例来存储具有不同实例的私有哈希表。我想

现在这更像是一个工厂模式,但这并不重要我的主要问题是关于线程安全问题。下面的代码是我到目前为止的代码。

这是保护内部哈希表访问的正确方法吗?感谢

的意见或建议。


Eric

朋友不可继承的类RuleManager

私人共享mInnerList As new HashTable

Private Sub New()

''隐藏构造函数

End Sub


公共共享ReadOnly属性实例(可选ByVal UniqueKey为

String ="")作为RuleManager

获取

如果mInnerList.Contains( UniqueKey)然后

返回DirectCast(mInnerList(UniqueKey),RuleManager)

结束如果


SyncLock mInnerList.SyncRoot

如果不是mInnerList.Contains(UniqueKey)那么

mInnerList(UniqueKey)=新的RuleManager

结束如果

返回DirectCast( mInnerList(UniqueKey),RuleManager)

结束SyncLock


结束获取

结束物业

结束class

解决方案

Eric,


您是否考虑过这些问题nes for your instance

property? (假设Imports System.Web.HttpContext和_Instance是你班级的私人会员声明。)


公共共享ReadOnly属性实例()作为RuleManager


''//检查一个会话变量是否符号



''//当前单身人士对象存在。

如果Current.Session(" RuleManager")什么都没有那么


''//创建一个新的对象实例。 />
_Instance =新的RuleManager


''//将对象存储在会话中,使用签名

声明

''//在请求时检索它。

Current.Session(" RuleManager")= _ Instance


否则


''//检索

会话中当前存在的实例。

_Instance = CType(Current.Session(" RuleManager")) ,

RuleManager)


结束如果


''//重新转动单身的单数会话实例,

是否

''//它是从会话中创建或检索的。

返回_Instance


结束功能


您基本上是为每个用户创建一个Singleton,并将
存储在Session中。你不必担心

现在交叉污染会议。


HTH,


Joseph


Joseph,


感谢您的建议。我会使用这种方法但是我现在拥有的dll还有RuleManager作为朋友的范围。这个类是我的dll的内部

。基本上我在dll中的所有类都需要能够使用此RuleManager工作

但是每个

会话的RuleManager可能需要不同。请记住,这个dll需要同时使用网络应用程序和winforms。


谢谢,


Eric

"约瑟夫费里斯 <乔*********** @ gmail.com>在消息中写道

news:11 ********************** @ g44g2000cwa.googlegr oups.com ...

Eric,

你有没有考虑过这些行的内容为你的实例
属性? (假设Imports System.Web.HttpContext和_Instance是您班级的私人会员声明。)

公共共享ReadOnly属性实例()作为RuleManager

''//检查是否存在具有
'//当前单例对象签名的会话变量。
如果Current.Session(" RuleManager")没有那么

'//创建对象的新实例。
_Instance =新的RuleManager

'//使用签名将对象存储在会话中
声明
'//在请求时检索它。
Current.Session(" RuleManager")= _Instance



>''//检索
会话中当前存在的实例。
_Instance = CType(Current.Session(" RuleManager"),
RuleManager)
结束如果

''//返回单例的单数会话实例,
是否//它是从会话中创建或检索的。
返回_Instance

结束功能

您基本上是为每个用户创建一个Singleton,并将其存储在Session中。你现在不必担心交叉污染会议。

HTH,

约瑟夫



Eric,


陷入困境。对不起,我无法获得进一步的帮助。


Joseph


I have a VB.net dll project with a class that is a singleton. I''ve been
using this in winform apps without any problems. I would like to use this
same dll in a web form project but my singleton will cause problems because
some sessions may need different values in the singleton. I want to change
my singleton to store a private hashtable with different instances. I guess
this is more like a factory pattern now but that doesn''t matter My main
concern is about thread safty. The code below is what I have so far. Is
this the correct way to protect access to the inner hashtable? Thanks for
the comments or suggestions.

Eric
Friend NotInheritable Class RuleManager
Private Shared mInnerList As new HashTable
Private Sub New()
''hide constructor
End Sub

Public Shared ReadOnly Property Instance(Optional ByVal UniqueKey As
String = "") As RuleManager
Get
If mInnerList.Contains(UniqueKey) Then
Return DirectCast(mInnerList(UniqueKey), RuleManager)
End If

SyncLock mInnerList.SyncRoot
If Not mInnerList.Contains(UniqueKey) Then
mInnerList(UniqueKey) = New RuleManager
End If
Return DirectCast(mInnerList(UniqueKey), RuleManager)
End SyncLock

End Get
End Property
End class

解决方案

Eric,

Have you considered something along these lines for your instance
property? (Assume Imports System.Web.HttpContext, and _Instance is a
private member declaration of your class.)

Public Shared ReadOnly Property Instance() As RuleManager

''// Check to see if a session variable with the signature of
the
''// current singleton object exists.
If Current.Session("RuleManager") Is Nothing Then

''// Create a new instance of the object.
_Instance = New RuleManager

''// Store the object in the session, using the signature
declared
''// to retrieve it when requested.
Current.Session("RuleManager") = _Instance

Else

''// Retrieve the instance that currently exists in the
session.
_Instance = CType(Current.Session("RuleManager"),
RuleManager)

End If

''// Return the singular session instance of the singleton,
whether
''// it was created or retrieved from the session.
Return _Instance

End Function

You are basically creating a Singleton for each individual user and
storing it in their Session. You don''t have to worry about
cross-polluting the session now.

HTH,

Joseph


Joseph,

Thanks for your suggestion. I would use that approach but the dll that I
currently have has the RuleManager scoped as Friend. This class is internal
to my dll. Basically all of my classes in the dll need to be able to work
with this RuleManager but the RuleManager may need to be different for each
session. Remember this dll needs to work with both web apps and winforms.

Thanks,

Eric
"Joseph Ferris" <jo***********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...

Eric,

Have you considered something along these lines for your instance
property? (Assume Imports System.Web.HttpContext, and _Instance is a
private member declaration of your class.)

Public Shared ReadOnly Property Instance() As RuleManager

''// Check to see if a session variable with the signature of
the
''// current singleton object exists.
If Current.Session("RuleManager") Is Nothing Then

''// Create a new instance of the object.
_Instance = New RuleManager

''// Store the object in the session, using the signature
declared
''// to retrieve it when requested.
Current.Session("RuleManager") = _Instance

Else

''// Retrieve the instance that currently exists in the
session.
_Instance = CType(Current.Session("RuleManager"),
RuleManager)

End If

''// Return the singular session instance of the singleton,
whether
''// it was created or retrieved from the session.
Return _Instance

End Function

You are basically creating a Singleton for each individual user and
storing it in their Session. You don''t have to worry about
cross-polluting the session now.

HTH,

Joseph



Eric,

Gotcha. Sorry I couldn''t be of further assistance.

Joseph


这篇关于线程安全单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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