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. NSOJ Constructing Roads(图论)

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

  2. python_基础学习_03_正则替换文本(re.sub)

    python的正则表达式模块是re,替换相关的方法是sub. 例如我们要做如下的替换将所有的 替换为空格,可以通过下面代码实现: import re input = 'hello world' #第一 ...

  3. UVA Don&#39;t Get Rooked

    主题如以下:  Don't Get Rooked  In chess, the rook is a piece that can move any number of squaresverticall ...

  4. 【百度地图API】让用户选择起点和终点的驾车导航

    原文:[百度地图API]让用户选择起点和终点的驾车导航 摘要: 如果用户搜索“从机场到火车站”,使用驾车导航DrivingRoute会默认显示一条结果.但同一个城市可能有多个机场和火车站,那么,如何用 ...

  5. ajax form表单提交 input file中的文件

    ajax form表单提交 input file中的文件 现今的主流浏览器由于ajax提交form表单无法把文件类型数据提交到后台,供后台处理,可是开发中由于某些原因又不得不用ajax提交文件, 为了 ...

  6. IOS开发计算文本尺寸

    在IOS开发中例如微博,QQ聊天界面中要显示大量的文字信息,这样需要计算出文字部分的尺寸,才能设计出合适的控件尺寸和位置.下面是IOS 7.0计算文本尺寸的方法.- (CGRect)boundingR ...

  7. c#之函数创建和闭包

    c#之函数创建和闭包 阅读目录: 动态创建函数 匿名函数不足之处 理解c#中的闭包 闭包的优点 动态创建函数 大多数同学,都或多或少的使用过.回顾下c#中动态创建函数的进化: C# 1.0中: pub ...

  8. 发现新大陆:一个最简单的破解SSL加密网络数据包的方法

    1. 简介 相信能访问到这篇文章的同行基本上都会用过流行的网络抓包工具WireShark,用它来抓取相应的网络数据包来进行问题分析或者其他你懂的之类的事情. 一般来说,我们用WireShark来抓取包 ...

  9. MySql状态查看方法 MySql如何查看连接数和状态?

    原文:MySql状态查看方法 MySql如何查看连接数和状态? 如果是root帐号,你能看到所有用户的当前连接.如果是其它普通帐号,只能看到自己占用的连接 怎么进入mysql命令行呢? mysql的安 ...

  10. Codeforces Round #FF 446A DZY Loves Sequences

    预处理出每一个数字能够向后延伸多少,然后尝试将两段拼起来. C. DZY Loves Sequences time limit per test 1 second memory limit per t ...