对象同步(收藏)

来源:互联网 发布:ubuntu切换搜狗输入法 编辑:程序博客网 时间:2024/04/29 20:37

system.runtime.remoting.contexts.synchronizationattribute

.NET Framework 类库?
SynchronizationAttribute 类?

为当前上下文和所有共享同一实例的上下文强制一个同步域。

命名空间:System.Runtime.Remoting.Contexts
程序集:mscorlib(在 mscorlib.dll 中)

语法语法
Visual Basic(声明)
<SerializableAttribute> _<AttributeUsageAttribute(AttributeTargets.Class)> _<ComVisibleAttribute(True)> _Public Class SynchronizationAttribute    Inherits ContextAttribute    Implements IContributeServerContextSink, IContributeClientContextSink
Visual Basic(用法)
Dim instance As SynchronizationAttribute
C#
[SerializableAttribute] [AttributeUsageAttribute(AttributeTargets.Class)] [ComVisibleAttribute(true)] public class SynchronizationAttribute : ContextAttribute, IContributeServerContextSink, IContributeClientContextSink
C++
[SerializableAttribute] [AttributeUsageAttribute(AttributeTargets::Class)] [ComVisibleAttribute(true)] public ref class SynchronizationAttribute : public ContextAttribute, IContributeServerContextSink, IContributeClientContextSink
J#
/** @attribute SerializableAttribute() */ /** @attribute AttributeUsageAttribute(AttributeTargets.Class) */ /** @attribute ComVisibleAttribute(true) */ public class SynchronizationAttribute extends ContextAttribute implements IContributeServerContextSink, IContributeClientContextSink
JScript
SerializableAttribute AttributeUsageAttribute(AttributeTargets.Class) ComVisibleAttribute(true) public class SynchronizationAttribute extends ContextAttribute implements IContributeServerContextSink, IContributeClientContextSink
备注备注

将该属性应用于某个对象时,在共享该属性实例的所有上下文中只能执行一个线程。这是通过提供为各个上下文截获并序列化传入调用的接收器来实现的。如果为重入标记了该属性,那么标注也将被截获。标注侦听允许其他等待线程进入该同步域以获得最大的吞吐量。

Note注意

存在两个名为 SynchronizationAttribute 的类:一个在 System.Runtime.Remoting.Contexts 命名空间中,另一个在 System.EnterpriseServices 命名空间中。System.EnterpriseServices.SynchronizationAttribute 类仅支持同步调用,并且只可与接受服务的组件一起使用。(有关接受服务的组件的更多信息,请参见 服务组件概述。)System.Runtime.Remoting.Contexts.SynchronizationAttribute 同时支持同步调用和异步调用,并且只可与上下文绑定对象一起使用。(有关上下文绑定对象的更多信息,请参见 ContextBoundObject 类。)

Note注意

此类在类级别生成链接要求和继承要求。如果直接调用方或继承类没有基础结构权限,则会引发 SecurityException。有关安全要求的详细信息,请参见 链接要求 和 继承要求。

示例示例

下面的代码示例阐释了 SynchronizationAttribute 的用法。有关完整的代码示例,请参见 AsyncResult 类的示例。

Visual Basic
' Context-bound type with the Synchronization context attribute.<Synchronization()> Public Class SampleSyncronized   Inherits ContextBoundObject      ' A method that does some work, and returns the square of the given number.   Public Function Square(i As Integer) As Integer            Console.Write("The hash of the thread executing ")      Console.WriteLine("SampleSyncronized.Square is: {0}", Thread.CurrentThread.GetHashCode())      Return i * i   End Function 'SquareEnd Class 'SampleSyncronized
C#
// Context-bound type with the Synchronization context attribute.[Synchronization()]public class SampleSyncronized : ContextBoundObject {    // A method that does some work, and returns the square of the given number.    public int Square(int i)  {        Console.Write("The hash of the thread executing ");        Console.WriteLine("SampleSyncronized.Square is: {0}",                              Thread.CurrentThread.GetHashCode());        return i*i;    }}
C++
// Context-bound type with the Synchronization context attribute.[Synchronization]public ref class SampleSyncronized: public ContextBoundObject{public:   // A method that does some work, and returns the square of the given number.   int Square( int i )   {      Console::Write( "The hash of the thread executing " );      Console::WriteLine( "SampleSyncronized::Square is: {0}", Thread::CurrentThread->GetHashCode() );      return i * i;   }};
J#
// Context-bound type with the Synchronization context attribute./** @attribute Synchronization() */public class SampleSyncronized extends ContextBoundObject{    // A method that does some work, and returns the square of the given number.    public int Square(int i)    {        Console.Write("The hash of the thread executing ");        Console.WriteLine("SampleSyncronized.Square is: {0}",            (Int32)System.Threading.Thread.get_CurrentThread().GetHashCode());        return i * i;    } //Square} //SampleSyncronized
.NET Framework 安全性.NET Framework 安全性
  • SecurityPermission? 用于操作基础结构代码。要求值:SecurityAction.LinkDemand;权限值:SecurityPermissionFlag.Infrastructure
  • SecurityPermission? 用于操作基础结构代码。要求值:SecurityAction.InheritanceDemand;权限值:SecurityPermissionFlag.Infrastructure
继承层次结构继承层次结构
System.Object
?? System.Attribute
???? System.Runtime.Remoting.Contexts.ContextAttribute
??????System.Runtime.Remoting.Contexts.SynchronizationAttribute
线程安全线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。

版本信息版本信息
 
原创粉丝点击