[MethodImpl(MethodImplOptions.Synchronized)]
2015-07-24 16:36
676 查看
在NopCommerce项目的Nop.Core类库中有一个EngineContext类中有一个Initialize方法用到了[MethodImpl(MethodImplOptions.Synchronized)]
我的理解它和Lock差不多,是解决多线程同步问题的;
该方法一次只能由一个线程执行。 静态方法锁定类型,而实例方法锁定实例。 在任何实例函数中只能执行一个线程,并且在类的任何静态函数中只能执行一个线程。
如果[MethodImplAttribute(MethodImplOptions.Synchronized)]被应用到instance method,相当于对当前实例加锁。
如果[MethodImplAttribute(MethodImplOptions.Synchronized)]被应用到static method,相当于当前类型加锁。
例子:http://www.cnblogs.com/artech/archive/2008/10/17/1313209.html
/// <summary> /// Initializes a static instance of the Nop factory. /// </summary> /// <param name="forceRecreate">Creates a new factory instance even though the factory has been previously initialized.</param> [MethodImpl(MethodImplOptions.Synchronized)] public static IEngine Initialize(bool forceRecreate) { if (Singleton<IEngine>.Instance == null || forceRecreate) { var config = ConfigurationManager.GetSection("NopConfig") as NopConfig; Singleton<IEngine>.Instance = CreateEngineInstance(config); Singleton<IEngine>.Instance.Initialize(config); } return Singleton<IEngine>.Instance; }
我的理解它和Lock差不多,是解决多线程同步问题的;
该方法一次只能由一个线程执行。 静态方法锁定类型,而实例方法锁定实例。 在任何实例函数中只能执行一个线程,并且在类的任何静态函数中只能执行一个线程。
如果[MethodImplAttribute(MethodImplOptions.Synchronized)]被应用到instance method,相当于对当前实例加锁。
如果[MethodImplAttribute(MethodImplOptions.Synchronized)]被应用到static method,相当于当前类型加锁。
实例方法锁定实例
class Program { //在入口Main方法中,创建SyncHelper对象,通过一个System.Threading.Timer对象实现每隔1s调用该对象的Execute方法: static void Main(string[] args) { SyncHelper sync = new SyncHelper(); Timer timer = new Timer(delegate { sync.Execute(); }, null, 0, 1000); Console.ReadKey(); } } class SyncHelper { public void Execute() { Console.WriteLine("Excute at {0}", DateTime.Now); Thread.Sleep(5000); } }
class Program { static void Main(string[] args) { SyncHelper sync = new SyncHelper(); Timer timer = new Timer(delegate { sync.Execute(); }, null, 0, 1000); Console.ReadKey(); } } class SyncHelper { [MethodImpl(MethodImplOptions.Synchronized)] public void Execute() { Console.WriteLine("Excute at {0}", DateTime.Now); Thread.Sleep(5000); } }
静态方法锁定类型
class Program { static void Main(string[] args) { SyncHelper sync = new SyncHelper(); Timer timer = new Timer(delegate { SyncHelper.Execute(); }, null, 0, 1000); Console.ReadKey(); } } class SyncHelper { [MethodImpl(MethodImplOptions.Synchronized)] public static void Execute() { Console.WriteLine("Excute at {0}", DateTime.Now); Thread.Sleep(5000); } }
例子:http://www.cnblogs.com/artech/archive/2008/10/17/1313209.html
相关文章推荐
- java.lang.NoClassDefFoundError: org/apache/commons/pool/KeyedObjectPoolFactory
- LintCode - Copy List with Random Pointer
- Linux D状态
- linux移植常见问题
- 【工作笔记0003】网站真假分页个人观点
- 使用 Jersey 和 Apache Tomcat 构建 RESTful Web 服务
- LR如何监控tomcat性能
- Nginx rewrite 多重判断
- 以Tomcat为例介绍Servlet容器启动过程
- 使用Scala开发Apache Kafka应用入门
- Web服务器虚拟目录
- cloudera hadoop相关源码下载
- LINUX 环境安装 jdk-tomcat安装
- linux popen函数
- OpenCV基于傅里叶变换进行文本的旋转校正 推荐
- Linux: /dev/random , /dev/urandom
- 关于tomcat+MyEclipse的安装与配置
- Linux Samba 安装及配置
- 专搜电子书网站
- shell编程