zoukankan      html  css  js  c++  java
  • 通过锁字符串达到控制并发的效果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();
            }
        }
    
    
    
    
    
    
    
    }

    运行效果图:

  • 相关阅读:
    mySQL优化方案
    java之自动过滤提交文本中的html代码script代码
    java小技术之生成二维码
    微信扫码支付功能详细教程————Java
    java实现发送邮件服务器,SMTP协议发送邮件
    『重构--改善既有代码的设计』读书笔记----序
    Linux导航神器-----autojump
    绘图时,根据size()和自定义rect编程的区别
    Qt中如何在QCursor移动的时候不触发moveEvent
    Qt远程机开发时光标注意问题
  • 原文地址:https://www.cnblogs.com/sharing1986687846/p/10300416.html
Copyright © 2011-2022 走看看