using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Linq.Expressions;
using System.Runtime.CompilerServices; namespace TestSyncMethodAttr
{
class SyncHelper
{
[MethodImpl(MethodImplOptions.Synchronized)]
public void Execute()
{
Console.WriteLine( "Thread Name:{1},Excute at {0}", DateTime.Now,Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(5000);
} //[MethodImpl(MethodImplOptions.Synchronized)]
public void ExecuteAnother()
{
Console.WriteLine("Thread Name:{1},Excute Another at {0}", DateTime.Now, Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(2000);
}
} delegate int del(int i); class Program
{
static void Main(string[] args)
{
SyncHelper helper = new SyncHelper(); TimerCallback func = (obj) => {
helper.Execute();
}; TimerCallback funcAnother = (obj) =>
{
helper.ExecuteAnother();
}; Timer timer = new Timer(func, null, 0, 1000); Timer timerAnother = new Timer(funcAnother, null, 0, 1000); /*
Expression<del> myET = x => x * x;
Console.WriteLine(myET.Compile().Invoke(2));
*/ /*
Timer timer = new Timer(
delegate
{
helper.Execute();
}, null, 0, 1000);
*/ Console.Read(); }
}
}

  

C#方法同步 [MethodImpl(MethodImplOptions.Synchronized)]的更多相关文章

  1. [MethodImpl(MethodImplOptions.Synchronized)]、lock(this)与lock(typeof(...))

    对于稍微有点经验的.NET开发人员来说,倘若被问及如何保持线程同步,我想很多人都能说好好几种.在众多的线程同步的可选方式中,加锁无疑是最为常用的.如果仅仅是基于方法级别的线程同步,使用System.R ...

  2. [MethodImpl(MethodImplOptions.Synchronized)]

    在NopCommerce项目的Nop.Core类库中有一个EngineContext类中有一个Initialize方法用到了[MethodImpl(MethodImplOptions.Synchron ...

  3. C# Note26: [MethodImpl(MethodImplOptions.Synchronized)]与lock机制

    在进行.NET开发时,经常会遇见如何保持线程同步的情况.在众多的线程同步的可选方式中,加锁无疑是最为常用的.如果仅仅是基于方法级别的线程同步,使用System.Runtime.CompilerServ ...

  4. [MethodImpl(MethodImplOptions.Synchronized)]与lock机制

    [MethodImpl(MethodImplOptions.Synchronized)]与lock机制 在进行.NET开发时,经常会遇见如何保持线程同步的情况.在众多的线程同步的可选方式中,加锁无疑是 ...

  5. java 多线程:线程通信-等待通知机制wait和notify方法;(同步代码块synchronized和while循环相互嵌套的差异);管道通信:PipedInputStream;PipedOutputStream;PipedWriter; PipedReader

    1.等待通知机制: 等待通知机制的原理和厨师与服务员的关系很相似: 1,厨师做完一道菜的时间不确定,所以厨师将菜品放到"菜品传递台"上的时间不确定 2,服务员什么时候可以取到菜,必 ...

  6. MethodImplOptions.Synchronized的一点讨论

    Review代码发现有一个方法加了[MethodImpl(MethodImplOptions.Synchronized)] 属性,这个属性的目的,从名字上就可以看出,是要对所有线程进行同步执行. 对方 ...

  7. C#多线程方法同步

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. 警惕32位程序在MethodImplOptions.Synchronized在x64机器上的同步缺陷[z]

    https://www.cnblogs.com/junchu25/archive/2012/08/10/2631422.html 上周四产品上线一切运行正常,做了一点小改动后周四晚上发布,周五大量用户 ...

  9. 深入理解synchronized方法同步的是方法还是对象?

    一.运用synchronized关键字 首先我们来看看一个多线程中线程不安全的列子 代码如下: 共享数据类: public class NotSynchronizated extends Thread ...

随机推荐

  1. ServiceStack.Redis——Redis于.net向上client解

    ServiceStack.Redis--Redis于.net向上client解 源代码和使用: https://github.com/ServiceStack/ServiceStack.Redis 样 ...

  2. android static达到Service与Activity于Handler沟通

    供static理解力,仅适用于static理解力,不喜勿喷 第一种方式:离Service中获取Activity的static变量,调用该静态变量的getHandler()获取Handler进行发送消息 ...

  3. AssetManager asset使用

    Android 该系统提供了一个程序为每个新的设计/assets文件夹.保存该文件在此文件夹可以在一个程序被打包./res 和/assets所不同的是,android不/assets下生成的文件ID. ...

  4. 接收终端Request.InputStream阅读

    接收终端Request.InputStream阅读请求页面参数,最后字符串. byte[] byts = new byte[HttpContext.Current.Request.InputStrea ...

  5. linux系统管理命令--系统测试工具

    1.tcpdump -n  使用IP地址表示主机,而不是主机名的:使用数字port号码,而不是服务名 -i    指定要监听的网卡接口, "-i any"表示抓取全部网卡接口上的数 ...

  6. Apache & WebDav 配置(二)

    (二)WebDav配置 & put脚本编辑! WebDav 配置 ---------------- # 1. 修改了两个配置文件 # 2. 创建web访问用户的用户名和口令 # 3. 创建了两 ...

  7. FindBugs:Compiler output path for module can not be null. check your module/project settings问题原因

    这可能是很多人在使用Android studio 该插件会发现此错误信息:Compiler output path for module can not be null. check your mod ...

  8. Web API 2 对 CORS 的支持

    Web API 2 对 CORS 的支持 CORS概念 跨域资源共享 (CORS) 是一种万维网联合会 (W3C) 规范(通常被认为是 HTML5 的一部分),它可让 JavaScript 克服由浏览 ...

  9. cocos2d-x 3.2 2048——第六部分(最后一章)

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  10. [Elasticsearch] 分布式文件存储

    本文翻译自Elasticsearch官方指南的distributed document store一章. 分布式文档存储 在上一章中,我们一直在介绍索引数据和获取数据的方法.可是我们省略了非常多关于数 ...