zoukankan      html  css  js  c++  java
  • 一个C#的加锁解锁示例

    大家可以仿照这个方法做:
     
    using   System; 
    using   System.Collections.Generic; 
    using   System.Text; 

    namespace   Generic_Reusable 

            interface   ILockable 
            { 
                    void   Lock(); 
                    void   Unlock(); 
            } 

            class   SharedMemoryLock   :   ILockable 
            { 
                    #region   ILockable   Members 

                    public   void   Lock() 
                    { 
                            Console.WriteLine( "SharedLock   performs   lock   method. "); 
                    } 

                    public   void   Unlock() 
                    { 
                            Console.WriteLine( "SharedLock   performs   unlock   method. "); 
                    } 

                    #endregion 
            } 

            class   FileLock   :   ILockable 
            { 
                    #region   ILockable   Members 

                    public   void   Lock() 
                    { 
                            Console.WriteLine( "FileLock   performs   lock   method. "); 
                    } 

                    public   void   Unlock() 
                    { 
                            Console.WriteLine( "FileLock   performs   unlock   method. "); 
                    } 

                    #endregion 
            } 

            class   ReusableLock <LOCK >   :   ILockable   where   LOCK   :   ILockable,   new() 
            { 
                    private   LOCK   lock__   =   new   LOCK(); 

                    #region   ILockable   Members 

                    public   void   Lock() 
                    { 
                            lock__.Lock(); 
                    } 

                    public   void   Unlock() 
                    { 
                            lock__.Unlock(); 
                    } 

                    #endregion 
            } 

            class   Program 
            { 
                    static   void   Main(string[]   args) 
                    { 
                            try 
                            { 
                                    ReusableLock <SharedMemoryLock >   theLock   =   new   ReusableLock <SharedMemoryLock >(); 
                                    theLock.Lock(); 
                                    //   TODO:   Add   your   code   here 
                                    theLock.Unlock(); 
                            } 
                            catch   (Exception   ex) 
                            { 
                                    Console.WriteLine(ex.Message); 
                            } 
                    } 
            } 
    作者: XuGang   网名:钢钢
    出处: http://xugang.cnblogs.com
    声明: 本文版权归作者和博客园共有。转载时必须保留此段声明,且在文章页面明显位置给出原文连接地址!
  • 相关阅读:
    使用反射获取对象的步骤
    金融IT的算法要求
    Java编译与反编译命令记录
    常用搜索博客/网站
    Java核心-03 谈谈final、finally、 finalize有什么不同?
    Java核心-02 Exception和Error有什么区别?
    PHP函数技巧篇
    IO
    Markdown 简单使用教程
    FZu Problem 2233 ~APTX4869 (并查集 + sort)
  • 原文地址:https://www.cnblogs.com/wwwbdabc/p/11653086.html
Copyright © 2011-2022 走看看