• 通过锁字符串达到控制并发的效果C#


    lock锁的是内存地址

    而.net有内部机制使得相同的字符串内存地址是相同的(new string)除外

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;
    
    namespace ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                List<string> keyList = new List<string> { "key1", "key2", "key1", "key1", "key1", "key1", };
    
                keyList.ForEach(u =>
                {
                    ThreadPool.QueueUserWorkItem(s =>
                    {
                        Test.lockTestByString(u);
                    });
    
                });
    
                Console.Read();
    
            }
        }
    
    
        public class Test
        {
    
            public static void lockTestByString(string key)
            {
                lock (key)
                {
                    GCHandle handle = GCHandle.Alloc(key, GCHandleType.Pinned);
                    //获取内存地址
                    IntPtr address = handle.AddrOfPinnedObject();
    
                    Console.WriteLine("上锁2s key=" + key + " 内存地址:" + address);
                    Thread.Sleep(2000);
                    Console.WriteLine("解锁");
    
                }
            }
    
        }
    
    
    }

    运行效果图:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading;
    
    namespace ConsoleApp
    {
        //class Program
        //{
        //    static void Main(string[] args)
        //    {
    
        //        List<string> keyList = new List<string> { "key1", "key2", "key1", "key1", "key1", "key1", };
    
        //        keyList.ForEach(u =>
        //        {
        //            ThreadPool.QueueUserWorkItem(s =>
        //            {
        //                Test.lockTestByString(u);
        //            });
    
        //        });
    
        //        Console.Read();
    
        //    }
        //}
    
    
        public class Test
        {
    
            public static void lockTestByString(string key)
            {
                lock (key)
                {
                    GCHandle handle = GCHandle.Alloc(key, GCHandleType.Pinned);
                    //获取内存地址
                    IntPtr address = handle.AddrOfPinnedObject();
    
                    Console.WriteLine("上锁2s key=" + key + " 内存地址:" + address);
                    Thread.Sleep(2000);
                    Console.WriteLine("解锁");
    
                }
            }
    
        }
    
    
        class Program
        {
            static void Main(string[] args)
            {
    
                List<string> keyList = new List<string> { new string('k', 1), new string('k', 1), new string('k', 1), new string('k', 1) };
    
                keyList.ForEach(u =>
                {
                    ThreadPool.QueueUserWorkItem(s =>
                    {
                        Test.lockTestByString(u);
                    });
    
                });
    
                Console.Read();
            }
        }
    
    
    
    
    
    
    
    }

    运行效果图:

  • 相关阅读:
    golang pprof 使用
    iostat相关参数说明——await:平均每次设备I/O操作的等待时间 (毫秒),如果%util接近 100%,说明产生的I/O请求太多...
    二分查找
    golang 切片copy复制和等号复制的区别
    维生素
    LinkedBlockingQueue
    ArrayBlockingQueue
    wordpress文章点击次数统计插件WP Postviews 使用方法
    windows7下cmd窗口使用ssh登录服务器(云、本地)
    WinISO Standard V6.4.1.6137 免费无限制版
  • 原文地址:https://www.cnblogs.com/sharing1986687846/p/10300416.html
Copyright © 2020-2023  润新知